Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

report better error messages after Diesel queries #907

Merged
merged 3 commits into from
Apr 13, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
clean up implementation
davepacheco committed Apr 13, 2022
commit bc738ba76faacd2afc1247a8dcf2cd5e99cc5b1e
7 changes: 6 additions & 1 deletion nexus/src/authz/api_resources.rs
Original file line number Diff line number Diff line change
@@ -68,7 +68,12 @@ pub trait ApiResource: Clone + Send + Sync + 'static {
pub trait ApiResourceError {
/// Returns an error as though this resource were not found, suitable for
/// use when an actor should not be able to see that this resource exists
fn not_found(&self) -> Error;
fn not_found(&self) -> Error {
self.lookup_type().clone().into_not_found(self.resource_type())
}

fn resource_type(&self) -> ResourceType;
fn lookup_type(&self) -> &LookupType;
}

impl<T: ApiResource + ApiResourceError + oso::PolarClass> AuthorizedResource
10 changes: 6 additions & 4 deletions nexus/src/authz/authz-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -344,10 +344,12 @@ fn do_authz_resource(
}

impl ApiResourceError for #resource_name {
fn not_found(&self) -> Error {
self.lookup_type
.clone()
.into_not_found(ResourceType::#resource_name)
fn resource_type(&self) -> ResourceType {
ResourceType::#resource_name
}

fn lookup_type(&self) -> &LookupType {
&self.lookup_type
}
}
})
43 changes: 19 additions & 24 deletions nexus/src/db/error.rs
Original file line number Diff line number Diff line change
@@ -121,17 +121,18 @@ pub fn public_error_from_diesel_pool(
) -> PublicError {
public_error_from_diesel_pool_helper(error, |error| match handler {
ErrorHandler::NotFoundByResource(resource) => {
public_error_from_diesel_lookup_helper(error, || {
resource.not_found()
})
public_error_from_diesel_lookup_helper(
error,
resource.resource_type(),
resource.lookup_type(),
)
}
ErrorHandler::NotFoundByLookup(resource_type, lookup_type) => {
public_error_from_diesel_lookup_helper(error, || {
PublicError::ObjectNotFound {
type_name: resource_type,
lookup_type,
}
})
public_error_from_diesel_lookup_helper(
error,
resource_type,
&lookup_type,
)
}
ErrorHandler::Conflict(resource_type, object_name) => {
public_error_from_diesel_create(error, resource_type, object_name)
@@ -171,27 +172,21 @@ where

/// Converts a Diesel error to an external error, handling "NotFound" using
/// `make_not_found_error`.
fn public_error_from_diesel_lookup_helper<F>(
fn public_error_from_diesel_lookup_helper(
error: DieselError,
make_not_found_error: F,
) -> PublicError
where
F: FnOnce() -> PublicError,
{
resource_type: ResourceType,
lookup_type: &LookupType,
) -> PublicError {
match error {
DieselError::NotFound => make_not_found_error(),
DieselError::NotFound => {
lookup_type.clone().into_not_found(resource_type)
}
DieselError::DatabaseError(kind, info) => {
PublicError::internal_error(&format_database_error(kind, &*info))
}
error => {
let context = match make_not_found_error() {
PublicError::ObjectNotFound { type_name, lookup_type } => {
format!("looking up {:?} {:?}", type_name, lookup_type)
}
_ => {
format!("during unknown operation")
}
};
let context =
format!("accessing {:?} {:?}", resource_type, lookup_type);
PublicError::internal_error(&format!(
"Unknown diesel error {}: {:#}",
context, error