Skip to content

return 404 rather than 401/403 when user can't even see the object #600

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

Merged
merged 7 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 24 additions & 10 deletions common/src/api/external/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub enum Error {
}

/** Indicates how an object was looked up (for an `ObjectNotFound` error) */
#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub enum LookupType {
/** a specific name was requested */
ByName(String),
Expand All @@ -78,6 +78,26 @@ pub enum LookupType {
Other(String),
}

impl LookupType {
/// Returns an ObjectNotFound error appropriate for the case where this
/// lookup failed
pub fn into_not_found(self, type_name: ResourceType) -> Error {
Error::ObjectNotFound { type_name, lookup_type: self }
}
}

impl From<&str> for LookupType {
fn from(name: &str) -> Self {
LookupType::ByName(name.to_owned())
}
}

impl From<&Name> for LookupType {
fn from(name: &Name) -> Self {
LookupType::from(name.as_str())
}
}

impl Error {
/**
* Returns whether the error is likely transient and could reasonably be
Expand All @@ -103,28 +123,22 @@ impl Error {
* name.
*/
pub fn not_found_by_name(type_name: ResourceType, name: &Name) -> Error {
Error::ObjectNotFound {
type_name,
lookup_type: LookupType::ByName(name.as_str().to_owned()),
}
LookupType::from(name).into_not_found(type_name)
}

/**
* Generates an [`Error::ObjectNotFound`] error for a lookup by object id.
*/
pub fn not_found_by_id(type_name: ResourceType, id: &Uuid) -> Error {
Error::ObjectNotFound { type_name, lookup_type: LookupType::ById(*id) }
LookupType::ById(*id).into_not_found(type_name)
}

/**
* Generates an [`Error::ObjectNotFound`] error for some other kind of
* lookup.
*/
pub fn not_found_other(type_name: ResourceType, message: String) -> Error {
Error::ObjectNotFound {
type_name,
lookup_type: LookupType::Other(message),
}
LookupType::Other(message).into_not_found(type_name)
}

/**
Expand Down
Loading