[API-1] feat: search route

This commit is contained in:
Miguel da Mota 2024-01-01 23:58:26 +01:00
parent 57588c7e13
commit 96a091f068
24 changed files with 388 additions and 127 deletions

View file

@ -9,6 +9,15 @@ pub struct ErrorResponse {
pub status: StatusCode,
}
impl ErrorResponse {
pub fn new(message: &str, status: StatusCode) -> Self {
ErrorResponse {
message: message.to_string(),
status,
}
}
}
impl Display for ErrorResponse {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "{}: {}", self.status, self.message)

View file

@ -19,23 +19,20 @@ pub fn get_user(req: HttpRequest) -> Result<Users, ErrorResponse> {
}
Err(e) => {
return Err(match e.kind() {
jsonwebtoken::errors::ErrorKind::ExpiredSignature => ErrorResponse {
message: "Not Authorized".to_string(),
status: StatusCode::UNAUTHORIZED,
},
_ => ErrorResponse {
message: e.to_string(),
status: StatusCode::INTERNAL_SERVER_ERROR,
},
jsonwebtoken::errors::ErrorKind::ExpiredSignature => {
ErrorResponse::new("Not Authorized", StatusCode::UNAUTHORIZED)
}
_ => ErrorResponse::new(
e.to_string().as_str(),
StatusCode::INTERNAL_SERVER_ERROR,
),
})
}
}
}
None => {
return Err(ErrorResponse {
message: "Not Authorized".to_string(),
status: StatusCode::UNAUTHORIZED,
});
}
None => Err(ErrorResponse::new(
"Not Authorized",
StatusCode::UNAUTHORIZED,
)),
}
}