Skip to content

Commit 3d132cc

Browse files
authored
return 404 rather than 401/403 when user can't even see the object (#600)
1 parent 2a760e3 commit 3d132cc

File tree

13 files changed

+528
-257
lines changed

13 files changed

+528
-257
lines changed

common/src/api/external/error.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub enum Error {
6868
}
6969

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

81+
impl LookupType {
82+
/// Returns an ObjectNotFound error appropriate for the case where this
83+
/// lookup failed
84+
pub fn into_not_found(self, type_name: ResourceType) -> Error {
85+
Error::ObjectNotFound { type_name, lookup_type: self }
86+
}
87+
}
88+
89+
impl From<&str> for LookupType {
90+
fn from(name: &str) -> Self {
91+
LookupType::ByName(name.to_owned())
92+
}
93+
}
94+
95+
impl From<&Name> for LookupType {
96+
fn from(name: &Name) -> Self {
97+
LookupType::from(name.as_str())
98+
}
99+
}
100+
81101
impl Error {
82102
/**
83103
* Returns whether the error is likely transient and could reasonably be
@@ -103,28 +123,22 @@ impl Error {
103123
* name.
104124
*/
105125
pub fn not_found_by_name(type_name: ResourceType, name: &Name) -> Error {
106-
Error::ObjectNotFound {
107-
type_name,
108-
lookup_type: LookupType::ByName(name.as_str().to_owned()),
109-
}
126+
LookupType::from(name).into_not_found(type_name)
110127
}
111128

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

119136
/**
120137
* Generates an [`Error::ObjectNotFound`] error for some other kind of
121138
* lookup.
122139
*/
123140
pub fn not_found_other(type_name: ResourceType, message: String) -> Error {
124-
Error::ObjectNotFound {
125-
type_name,
126-
lookup_type: LookupType::Other(message),
127-
}
141+
LookupType::Other(message).into_not_found(type_name)
128142
}
129143

130144
/**

0 commit comments

Comments
 (0)