log error messages in web paths

HTTP requests were only returning the error message to the caller, not
logging locally. In most cases the problem could be understood
client-side, but there are some exceptions. E.g. if Moonfire returns
a 403 on WebSocket update, even in the Chrome debug tools's network
tab the HTTP response body seems to be unavailable. And in general,
it's nice to have more context server-side.

Logging a `response::Body` isn't practical (it could be a stream), so
convert all the web stuff to use `base::Error` err returns.

Convert the `METHOD_NOT_ALLOWED` paths to return `Ok` for now. This is a
bit lame but punts on having some way of plumbing an explicit/overridden
status code in `base::Error`, as no gRPC error kind cleanly maps to
that.

Also convert `db::auth`, rather than making up an error kind in the web
layer.

This is also a small step toward getting rid of `failure::Error`.
This commit is contained in:
Scott Lamb
2023-07-09 07:45:41 -07:00
parent ed7ab5dddf
commit 6a5b751bd6
11 changed files with 307 additions and 279 deletions

View File

@@ -29,6 +29,12 @@ pub struct Error {
}
impl Error {
pub fn wrap<E: Into<failure::Error>>(kind: ErrorKind, e: E) -> Self {
Self {
inner: e.into().context(kind),
}
}
pub fn kind(&self) -> ErrorKind {
*self.inner.get_context()
}
@@ -146,7 +152,7 @@ where
/// Like `failure::bail!`, but the first argument specifies a type as an `ErrorKind`.
///
/// Example:
/// Example with positional arguments:
/// ```
/// use moonfire_base::bail_t;
/// let e = || -> Result<(), moonfire_base::Error> {
@@ -155,10 +161,21 @@ where
/// assert_eq!(e.kind(), moonfire_base::ErrorKind::Unauthenticated);
/// assert_eq!(e.to_string(), "Unauthenticated: unknown user: slamb");
/// ```
///
/// Example with named arguments:
/// ```
/// use moonfire_base::bail_t;
/// let e = || -> Result<(), moonfire_base::Error> {
/// let user = "slamb";
/// bail_t!(Unauthenticated, "unknown user: {user}");
/// }().unwrap_err();
/// assert_eq!(e.kind(), moonfire_base::ErrorKind::Unauthenticated);
/// assert_eq!(e.to_string(), "Unauthenticated: unknown user: slamb");
/// ```
#[macro_export]
macro_rules! bail_t {
($t:ident, $e:expr) => {
return Err($crate::Error::from(failure::err_msg($e).context($crate::ErrorKind::$t)).into());
($t:ident, $fmt:expr) => {
return Err($crate::Error::from(failure::err_msg(format!($fmt)).context($crate::ErrorKind::$t)).into());
};
($t:ident, $fmt:expr, $($arg:tt)+) => {
return Err($crate::Error::from(failure::err_msg(format!($fmt, $($arg)+)).context($crate::ErrorKind::$t)).into());