From 0c9747705eb6a43a5a8c7d9bb5a4eb3f771d3063 Mon Sep 17 00:00:00 2001 From: Roger Leigh Date: Thu, 13 Jul 2006 19:07:16 +0000 Subject: * TODO: Remove completed error string markup item. * Add boost::format markup to error strings. * sbuild/sbuild-custom-error.h: All constructor detail arguments are now templated. Add additional constructors for multiple detail arguments. --- ChangeLog | 10 +++++++ TODO | 6 ---- sbuild/sbuild-auth-conv-tty.cc | 6 ++-- sbuild/sbuild-auth.cc | 15 ++++++---- sbuild/sbuild-chroot.cc | 2 +- sbuild/sbuild-custom-error.h | 65 ++++++++++++++++++++++++++++++++---------- sbuild/sbuild-dirstream.cc | 4 +-- sbuild/sbuild-keyfile.cc | 2 +- sbuild/sbuild-lock.cc | 15 ++++------ sbuild/sbuild-personality.cc | 2 +- sbuild/sbuild-run-parts.cc | 2 +- sbuild/sbuild-session.cc | 34 ++++++++++------------ 12 files changed, 98 insertions(+), 65 deletions(-) diff --git a/ChangeLog b/ChangeLog index fb9427da..a80b43bd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2006-07-13 Roger Leigh + + * TODO: Remove completed error string markup item. + + * Add boost::format markup to error strings. + + * sbuild/sbuild-custom-error.h: All constructor detail arguments + are now templated. Add additional constructors for multiple detail + arguments. + 2006-07-13 Roger Leigh * sbuild/sbuild-chroot.h: API documentation corrections. diff --git a/TODO b/TODO index 8625395d..1ceb3858 100644 --- a/TODO +++ b/TODO @@ -17,12 +17,6 @@ PENDING the boost::format markup changes fixing first. What are other projects doing? -* Use boost::format markup in the standard error strings. - - This will complete the i18n support for error translation. It will - also allow templated exceptions which will simplify error reporting - for non-string types. - PLANNED ------- diff --git a/sbuild/sbuild-auth-conv-tty.cc b/sbuild/sbuild-auth-conv-tty.cc index a730f546..651c5c5b 100644 --- a/sbuild/sbuild-auth-conv-tty.cc +++ b/sbuild/sbuild-auth-conv-tty.cc @@ -49,7 +49,7 @@ namespace emap(auth_conv_tty::TIMEOUT, N_("Timed out")), emap(auth_conv_tty::TIMEOUT_PENDING, N_("Time is running out...")), emap(auth_conv_tty::TERMIOS, N_("Failed to get terminal settings")), - emap(auth_conv_tty::CONV_TYPE, N_("Unsupported conversation type")) + emap(auth_conv_tty::CONV_TYPE, N_("Unsupported conversation type \"%1%\"")) }; volatile sig_atomic_t timer_expired = false; @@ -295,9 +295,7 @@ auth_conv_tty::conversation (message_list& messages) break; default: { - format fmt("%1%"); - fmt % cur->type; - error e(fmt.str(), CONV_TYPE); + error e(cur->type, CONV_TYPE); log_error() << e.what() << endl; return false; } diff --git a/sbuild/sbuild-auth.cc b/sbuild/sbuild-auth.cc index df60334d..2106c04c 100644 --- a/sbuild/sbuild-auth.cc +++ b/sbuild/sbuild-auth.cc @@ -51,7 +51,7 @@ namespace emap init_errors[] = { emap(auth::HOSTNAME, N_("Failed to get hostname")), - emap(auth::USER, N_("User not found")), + emap(auth::USER, N_("User \"%1%\" not found")), emap(auth::AUTHENTICATION, N_("Authentication failed")), emap(auth::AUTHORISATION, N_("Access not authorised")), emap(auth::PAM_DOUBLE_INIT, N_("PAM is already initialised")), @@ -141,10 +141,10 @@ auth::auth (std::string const& service_name): struct passwd *pwent = getpwuid(this->ruid); if (pwent == 0) { - // TODO: Convert to using a lexical cast. - std::ostringstream str; - str << this->ruid; - throw error(str.str(), USER, strerror(errno)); + if (errno) + throw error(this->ruid, USER, strerror(errno)); + else + throw error(this->ruid, USER); } this->ruser = pwent->pw_name; @@ -201,7 +201,10 @@ auth::set_user (std::string const& user) struct passwd *pwent = getpwnam(this->user.c_str()); if (pwent == 0) { - throw error(user, USER, strerror(errno)); + if (errno) + throw error(user, USER, strerror(errno)); + else + throw error(user, USER); } this->uid = pwent->pw_uid; this->gid = pwent->pw_gid; diff --git a/sbuild/sbuild-chroot.cc b/sbuild/sbuild-chroot.cc index cc8c6933..00867e7a 100644 --- a/sbuild/sbuild-chroot.cc +++ b/sbuild/sbuild-chroot.cc @@ -57,7 +57,7 @@ namespace { emap(sbuild::chroot::CHROOT_CREATE, N_("Chroot creation failed")), emap(sbuild::chroot::CHROOT_DEVICE, N_("Device name not set")), - emap(sbuild::chroot::CHROOT_TYPE, N_("Unknown chroot type")), + emap(sbuild::chroot::CHROOT_TYPE, N_("Unknown chroot type \"%1%\"")), emap(sbuild::chroot::DEVICE_ABS, N_("Device must have an absolute path")), emap(sbuild::chroot::DEVICE_LOCK, N_("Failed to lock device")), emap(sbuild::chroot::DEVICE_NOTBLOCK, N_("File is not a block device")), diff --git a/sbuild/sbuild-custom-error.h b/sbuild/sbuild-custom-error.h index 29d37e06..02c26e06 100644 --- a/sbuild/sbuild-custom-error.h +++ b/sbuild/sbuild-custom-error.h @@ -48,12 +48,13 @@ namespace sbuild /** * The constructor. * - * @param detail the details of the error. + * @param context the context of the error. * @param error the error code. */ - custom_error (std::string const& detail, + template + custom_error (C const& context, error_type error): - sbuild::error(format_error(detail, null(), null(), error, null(), null())) + sbuild::error(format_error(context, null(), null(), error, null(), null())) { } @@ -61,32 +62,65 @@ namespace sbuild * The constructor. * * @param error the error code. - * @param error_string the error string. + * @param detail the details of the error. */ - custom_error (error_type error, - std::string const& error_string): - sbuild::error(format_error(null(), null(), null(), error, error_string, null())) + template + custom_error (error_type error, + D const& detail): + sbuild::error(format_error(null(), null(), null(), error, detail, null())) { } /** * The constructor. * + * @param error the error code. * @param detail the details of the error. + * @param detail2 additional details of the error. + */ + template + custom_error (error_type error, + D const& detail, + E const& detail2): + sbuild::error(format_error(null(), null(), null(), error, detail, detail2)) + { + } + + /** + * The constructor. + * + * @param context the context of the error. * @param error the error code. - * @param error_string the error string. + * @param detail the details of the error. */ - custom_error (std::string const& detail, - error_type error, - std::string const& error_string): - sbuild::error(format_error(detail, null(), null(), error, error_string, null())) + template + custom_error (C const& context, + error_type error, + D const& detail): + sbuild::error(format_error(context, null(), null(), error, detail, null())) { } /** * The constructor. * + * @param context the context of the error. + * @param error the error code. * @param detail the details of the error. + * @param detail2 additional details of the error. + */ + template + custom_error (C const& context, + error_type error, + D const& detail, + E const& detail2): + sbuild::error(format_error(context, null(), null(), error, detail, detail2)) + { + } + + /** + * The constructor. + * * @param error the error code. */ custom_error (std::runtime_error const& error): @@ -97,12 +131,13 @@ namespace sbuild /** * The constructor. * - * @param detail the details of the error. + * @param context the context of the error. * @param error the error code. */ - custom_error (std::string const& detail, + template + custom_error (C const& context, std::runtime_error const& error): - sbuild::error(format_error(detail, null(), null(), error, null(), null())) + sbuild::error(format_error(context, null(), null(), error, null(), null())) { } diff --git a/sbuild/sbuild-dirstream.cc b/sbuild/sbuild-dirstream.cc index 71b50a15..64ee83ad 100644 --- a/sbuild/sbuild-dirstream.cc +++ b/sbuild/sbuild-dirstream.cc @@ -37,8 +37,8 @@ namespace */ emap init_errors[] = { - emap(dirstream::DIR_OPEN, N_("Failed to open directory")), - emap(dirstream::DIR_READ, N_("Failed to read directory")) + emap(dirstream::DIR_OPEN, N_("Failed to open directory \"%1%\"")), + emap(dirstream::DIR_READ, N_("Failed to read directory \"%1%\"")) }; } diff --git a/sbuild/sbuild-keyfile.cc b/sbuild/sbuild-keyfile.cc index ac0e10d4..4d356b76 100644 --- a/sbuild/sbuild-keyfile.cc +++ b/sbuild/sbuild-keyfile.cc @@ -39,7 +39,7 @@ namespace */ emap init_errors[] = { - emap(keyfile::BAD_FILE, N_("Can't open file")), + emap(keyfile::BAD_FILE, N_("Can't open file \"%4%\"")), emap(keyfile::DISALLOWED_KEY, N_("line %1% [%2%]: Disallowed key \"%4%\"used")), emap(keyfile::DISALLOWED_KEY_NL, N_("[%2%]: Disallowed key \"%4%\"used")), emap(keyfile::DUPLICATE_GROUP, N_("line %1%: Duplicate group \"%4%\"")), diff --git a/sbuild/sbuild-lock.cc b/sbuild/sbuild-lock.cc index c2af7966..9efb55c5 100644 --- a/sbuild/sbuild-lock.cc +++ b/sbuild/sbuild-lock.cc @@ -47,13 +47,13 @@ namespace emap(lock::TIMEOUT_HANDLER, N_("Failed to set timeout handler")), emap(lock::TIMEOUT_SET, N_("Failed to set timeout")), emap(lock::TIMEOUT_CANCEL, N_("Failed to cancel timeout")), - emap(lock::LOCK, N_("Failed to acquire lock (timed out)")), - emap(lock::LOCK_TIMEOUT, N_("Failed to acquire lock")), + emap(lock::LOCK, N_("Failed to acquire lock")), + emap(lock::LOCK_TIMEOUT, N_("Failed to acquire lock (timed out after %4% seconds)")), emap(lock::DEVICE_LOCK, N_("Failed to acquire device lock")), - emap(lock::DEVICE_LOCK_TIMEOUT, N_("Failed to acquire device lock (timed out)")), + emap(lock::DEVICE_LOCK_TIMEOUT, N_("Failed to acquire device lock (timed out after %4% seconds; lock held by PID %5%)")), emap(lock::DEVICE_TEST, N_("Failed to test device lock")), emap(lock::DEVICE_RELEASE, N_("Failed to release device lock")), - emap(lock::DEVICE_RELEASE_TIMEOUT, N_("Failed to release device lock (timed out)")) + emap(lock::DEVICE_RELEASE_TIMEOUT, N_("Failed to release device lock (timed out after %4% seconds; lock held by PID %5%)")) }; } @@ -182,7 +182,7 @@ file_lock::set_lock (type lock_type, &read_lock) == -1) { if (errno == EINTR) - throw error(LOCK_TIMEOUT); + throw error(LOCK_TIMEOUT, timeout); else throw error(LOCK, strerror(errno)); } @@ -269,12 +269,9 @@ device_lock::set_lock (type lock_type, if (lock_timeout) { - - format fmt(_("lock held by pid %1%")); - fmt % status; throw error(((lock_type == LOCK_SHARED || lock_type == LOCK_EXCLUSIVE) ? DEVICE_LOCK_TIMEOUT : DEVICE_RELEASE_TIMEOUT), - fmt.str()); + timeout, status); } unset_timer(); } diff --git a/sbuild/sbuild-personality.cc b/sbuild/sbuild-personality.cc index 6a48fd35..045dbc3a 100644 --- a/sbuild/sbuild-personality.cc +++ b/sbuild/sbuild-personality.cc @@ -45,7 +45,7 @@ namespace */ emap init_errors[] = { - emap(sbuild::personality::SET, N_("Failed to set personality")) + emap(sbuild::personality::SET, N_("Failed to set personality \"%1%\"")) }; typedef std::pair pmap; diff --git a/sbuild/sbuild-run-parts.cc b/sbuild/sbuild-run-parts.cc index 58cced58..b702192f 100644 --- a/sbuild/sbuild-run-parts.cc +++ b/sbuild/sbuild-run-parts.cc @@ -47,7 +47,7 @@ namespace { emap(run_parts::CHILD_FORK, N_("Failed to fork child")), emap(run_parts::CHILD_WAIT, N_("Wait for child failed")), - emap(run_parts::EXEC, N_("Failed to execute")) + emap(run_parts::EXEC, N_("Failed to execute \"%1%\"")) }; } diff --git a/sbuild/sbuild-session.cc b/sbuild/sbuild-session.cc index 8fbda96e..b95fcd78 100644 --- a/sbuild/sbuild-session.cc +++ b/sbuild/sbuild-session.cc @@ -58,33 +58,33 @@ namespace */ emap init_errors[] = { - emap(session::CHDIR, N_("Failed to change to directory")), - emap(session::CHDIR_FB, N_("Falling back to directory")), + emap(session::CHDIR, N_("Failed to change to directory \"%1%\"")), + emap(session::CHDIR_FB, N_("Falling back to directory \"%4%\"")), emap(session::CHILD_CORE, N_("Child dumped core")), emap(session::CHILD_FAIL, N_("Child exited abnormally (reason unknown; not a signal or core dump)")), emap(session::CHILD_FORK, N_("Failed to fork child")), - emap(session::CHILD_SIGNAL, N_("Child terminated by signal")), + emap(session::CHILD_SIGNAL, N_("Child terminated by signal \"%4%\"")), emap(session::CHILD_WAIT, N_("Wait for child failed")), - emap(session::CHROOT, N_("Failed to change root to directory")), - emap(session::CHROOT_ALIAS, N_("No chroot found matching alias")), + emap(session::CHROOT, N_("Failed to change root to directory \"%1%\"")), + emap(session::CHROOT_ALIAS, N_("No chroot found matching name or alias \"%1%\"")), emap(session::CHROOT_LOCK, N_("Failed to lock chroot")), emap(session::CHROOT_SETUP, N_("Chroot setup failed")), - emap(session::CHROOT_UNKNOWN, N_("Failed to find chroot")), + emap(session::CHROOT_UNKNOWN, N_("Failed to find chroot \"%1%\"")), emap(session::CHROOT_UNLOCK, N_("Failed to unlock chroot")), - emap(session::COMMAND_ABS, N_("Command must have an absolute path")), - emap(session::EXEC, N_("Failed to execute")), + emap(session::COMMAND_ABS, N_("Command \"%1%\" must have an absolute path")), + emap(session::EXEC, N_("Failed to execute \"%1%\"")), emap(session::GROUP_GET_SUP, N_("Failed to get supplementary groups")), emap(session::GROUP_GET_SUPC, N_("Failed to get supplementary group count")), - emap(session::GROUP_SET, N_("Failed to set group")), + emap(session::GROUP_SET, N_("Failed to set group \"%1%\"")), emap(session::GROUP_SET_SUP, N_("Failed to set supplementary groups")), - emap(session::GROUP_UNKNOWN, N_("Group not found")), + emap(session::GROUP_UNKNOWN, N_("Group \"%1%\" not found")), emap(session::PAM, N_("PAM error")), emap(session::ROOT_DROP, N_("Failed to drop root permissions")), - emap(session::SHELL, N_("Shell not available")), - emap(session::SHELL_FB, N_("Falling back to shell")), + emap(session::SHELL, N_("Shell \"%1%\" not available")), + emap(session::SHELL_FB, N_("Falling back to shell \"%4%\"")), emap(session::SIGHUP_CATCH, N_("Caught hangup signal")), emap(session::SIGHUP_SET, N_("Failed to set hangup signal handler")), - emap(session::USER_SET, N_("Failed to set user")), + emap(session::USER_SET, N_("Failed to set user \"%1%\"")), emap(session::USER_SWITCH, N_("User switching is not permitted")), }; @@ -903,9 +903,7 @@ session::run_child (sbuild::chroot::ptr& session_chroot) /* Set group ID and supplementary groups */ if (setgid (get_gid())) { - std::ostringstream str; - str << get_gid(); - throw error(str.str(), GROUP_SET, strerror(errno)); + throw error(get_gid(), GROUP_SET, strerror(errno)); } if (initgroups (get_user().c_str(), get_gid())) { @@ -929,9 +927,7 @@ session::run_child (sbuild::chroot::ptr& session_chroot) /* Set uid and check we are not still root */ if (setuid (get_uid())) { - std::ostringstream str; - str << get_uid(); - throw error(str.str(), USER_SET); + throw error(get_uid(), USER_SET, strerror(errno)); } if (!setuid (0) && get_uid()) { -- cgit v1.2.3