Skip to content

Commit

Permalink
Add Error::not_found variant (#5242)
Browse files Browse the repository at this point in the history
Add a variant to our external Error so that a 404 can be returned
without requiring a ResourceType - adding one may not make sense for a
particular endpoint.
  • Loading branch information
jmpesp authored Mar 12, 2024
1 parent 145f25c commit 2d8bdf9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
29 changes: 29 additions & 0 deletions common/src/api/external/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ pub enum Error {

#[error("Conflict: {}", .message.display_internal())]
Conflict { message: MessagePair },

/// A generic 404 response. If there is an applicable ResourceType, use
/// ObjectNotFound instead.
#[error("Not found: {}", .message.display_internal())]
NotFound { message: MessagePair },
}

/// Represents an error message which has an external component, along with
Expand Down Expand Up @@ -199,6 +204,7 @@ impl Error {
| Error::InsufficientCapacity { .. }
| Error::InternalError { .. }
| Error::TypeVersionMismatch { .. }
| Error::NotFound { .. }
| Error::Conflict { .. } => false,
}
}
Expand Down Expand Up @@ -300,6 +306,15 @@ impl Error {
Error::Conflict { message: MessagePair::new(message.into()) }
}

/// Generates an [`Error::NotFound`] with a specific message.
///
/// This is used in cases where a generic 404 is required. For cases where
/// there is a ResourceType, use a function that produces
/// [`Error::ObjectNotFound`] instead.
pub fn non_resourcetype_not_found(message: impl Into<String>) -> Error {
Error::NotFound { message: MessagePair::new(message.into()) }
}

/// Given an [`Error`] with an internal message, return the same error with
/// `context` prepended to it to provide more context
///
Expand Down Expand Up @@ -354,6 +369,9 @@ impl Error {
Error::Conflict { message } => Error::Conflict {
message: message.with_internal_context(context),
},
Error::NotFound { message } => Error::NotFound {
message: message.with_internal_context(context),
},
}
}
}
Expand Down Expand Up @@ -475,6 +493,17 @@ impl From<Error> for HttpError {
internal_message,
}
}

Error::NotFound { message } => {
let (internal_message, external_message) =
message.into_internal_external();
HttpError {
status_code: http::StatusCode::NOT_FOUND,
error_code: Some(String::from("Not Found")),
external_message,
internal_message,
}
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion nexus/src/app/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ impl super::Nexus {
| Error::ServiceUnavailable { .. }
| Error::InsufficientCapacity { .. }
| Error::TypeVersionMismatch { .. }
| Error::Conflict { .. } => {
| Error::Conflict { .. }
| Error::NotFound { .. } => {
Reason::UnknownError { source: error }
}
})?;
Expand Down

0 comments on commit 2d8bdf9

Please sign in to comment.