Skip to content

Commit

Permalink
return 404 rather than 401/403 when user can't even see the object (#600
Browse files Browse the repository at this point in the history
)
  • Loading branch information
davepacheco authored Jan 18, 2022
1 parent 2a760e3 commit 3d132cc
Show file tree
Hide file tree
Showing 13 changed files with 528 additions and 257 deletions.
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

0 comments on commit 3d132cc

Please sign in to comment.