return structured errors from commands (wip)

This commit is contained in:
2022-12-23 11:34:17 -08:00
parent 2943634248
commit df6b362a31
8 changed files with 82 additions and 62 deletions

View File

@@ -1,4 +1,4 @@
use thiserror::Error;
use thiserror::Error as ThisError;
use aws_sdk_sts::{
types::SdkError as AwsSdkError,
@@ -9,9 +9,34 @@ use sqlx::{
migrate::MigrateError,
};
use serde::{Serialize, Serializer, ser::SerializeMap};
pub struct SerializeError<E> {
pub err: E
}
impl<E: std::error::Error> Serialize for SerializeError<E> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?;
map.serialize_entry("msg", &format!("{}", self.err))?;
if let Some(src) = self.err.source() {
let ser_src = SerializeError { err: src };
map.serialize_entry("source", &ser_src)?;
}
map.end()
}
}
impl<E: std::error::Error> From<E> for SerializeError<E> {
fn from(err: E) -> Self {
SerializeError { err }
}
}
// error during initial setup (primarily loading state from db)
#[derive(Debug, Error)]
#[derive(Debug, ThisError)]
pub enum SetupError {
#[error("Invalid database record")]
InvalidRecord, // e.g. wrong size blob for nonce or salt
@@ -25,7 +50,7 @@ pub enum SetupError {
// error when attempting to tell a request handler whether to release or deny credentials
#[derive(Debug, Error)]
#[derive(Debug, ThisError)]
pub enum SendResponseError {
#[error("The specified credentials request was not found")]
NotFound, // no request with the given id
@@ -35,12 +60,12 @@ pub enum SendResponseError {
// errors encountered while handling an HTTP request
#[derive(Debug, Error)]
#[derive(Debug, ThisError)]
pub enum RequestError {
#[error("Error writing to stream: {0}")]
StreamIOError(#[from] std::io::Error),
#[error("Received invalid UTF-8 in request")]
InvalidUtf8,
// #[error("Received invalid UTF-8 in request")]
// InvalidUtf8,
// MalformedHttpRequest,
#[error("HTTP request too large")]
RequestTooLarge,
@@ -55,7 +80,7 @@ pub enum RequestError {
}
#[derive(Debug, Error)]
#[derive(Debug, ThisError)]
pub enum GetCredentialsError {
#[error("Credentials are currently locked")]
Locked,
@@ -64,7 +89,7 @@ pub enum GetCredentialsError {
}
#[derive(Debug, Error)]
#[derive(Debug, ThisError)]
pub enum GetSessionError {
#[error("Request completed successfully but no credentials were returned")]
NoCredentials, // SDK returned successfully but credentials are None
@@ -73,7 +98,7 @@ pub enum GetSessionError {
}
#[derive(Debug, Error)]
#[derive(Debug, ThisError)]
pub enum UnlockError {
#[error("App is not locked")]
NotLocked,
@@ -91,7 +116,7 @@ pub enum UnlockError {
// Errors encountered while trying to figure out who's on the other end of a request
#[derive(Debug, Error)]
#[derive(Debug, ThisError)]
pub enum ClientInfoError {
#[error("Found PID for client socket, but no corresponding process")]
ProcessNotFound,