Update error codes on the api key routes

This commit is contained in:
Loïc Lecrenier 2023-01-08 13:03:23 +01:00 committed by Tamo
parent 96105a5e8d
commit 9ab791bedc
5 changed files with 114 additions and 18 deletions

View file

@ -230,7 +230,13 @@ pub enum Code {
MissingPayload,
ApiKeyNotFound,
MissingParameter,
MissingApiKeyActions,
MissingApiKeyExpiresAt,
MissingApiKeyIndexes,
InvalidApiKeyOffset,
InvalidApiKeyLimit,
InvalidApiKeyActions,
InvalidApiKeyIndexes,
InvalidApiKeyExpiresAt,
@ -362,7 +368,25 @@ impl Code {
// error related to keys
ApiKeyNotFound => ErrCode::invalid("api_key_not_found", StatusCode::NOT_FOUND),
MissingParameter => ErrCode::invalid("missing_parameter", StatusCode::BAD_REQUEST),
MissingApiKeyExpiresAt => {
ErrCode::invalid("missing_api_key_expires_at", StatusCode::BAD_REQUEST)
}
MissingApiKeyActions => {
ErrCode::invalid("missing_api_key_actions", StatusCode::BAD_REQUEST)
}
MissingApiKeyIndexes => {
ErrCode::invalid("missing_api_key_indexes", StatusCode::BAD_REQUEST)
}
InvalidApiKeyOffset => {
ErrCode::invalid("invalid_api_key_offset", StatusCode::BAD_REQUEST)
}
InvalidApiKeyLimit => {
ErrCode::invalid("invalid_api_key_limit", StatusCode::BAD_REQUEST)
}
InvalidApiKeyActions => {
ErrCode::invalid("invalid_api_key_actions", StatusCode::BAD_REQUEST)
}

View file

@ -60,7 +60,7 @@ impl Key {
.map(|act| {
from_value(act.clone()).map_err(|_| Error::InvalidApiKeyActions(act.clone()))
})
.ok_or(Error::MissingParameter("actions"))??;
.ok_or(Error::MissingApiKeyActions)??;
let indexes = value
.get("indexes")
@ -75,12 +75,12 @@ impl Key {
.collect()
})
})
.ok_or(Error::MissingParameter("indexes"))??;
.ok_or(Error::MissingApiKeyIndexes)??;
let expires_at = value
.get("expiresAt")
.map(parse_expiration_date)
.ok_or(Error::MissingParameter("expiresAt"))??;
.ok_or(Error::MissingApiKeyExpiresAt)??;
let created_at = OffsetDateTime::now_utc();
let updated_at = created_at;
@ -344,8 +344,12 @@ pub mod actions {
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("`{0}` field is mandatory.")]
MissingParameter(&'static str),
#[error("`expiresAt` field is mandatory.")]
MissingApiKeyExpiresAt,
#[error("`indexes` field is mandatory.")]
MissingApiKeyIndexes,
#[error("`actions` field is mandatory.")]
MissingApiKeyActions,
#[error("`actions` field value `{0}` is invalid. It should be an array of string representing action names.")]
InvalidApiKeyActions(Value),
#[error("`indexes` field value `{0}` is invalid. It should be an array of string representing index names.")]
@ -375,7 +379,9 @@ impl From<IndexUidFormatError> for Error {
impl ErrorCode for Error {
fn error_code(&self) -> Code {
match self {
Self::MissingParameter(_) => Code::MissingParameter,
Self::MissingApiKeyExpiresAt => Code::MissingApiKeyExpiresAt,
Self::MissingApiKeyIndexes => Code::MissingApiKeyIndexes,
Self::MissingApiKeyActions => Code::MissingApiKeyActions,
Self::InvalidApiKeyActions(_) => Code::InvalidApiKeyActions,
Self::InvalidApiKeyIndexes(_) | Self::InvalidApiKeyIndexUid(_) => {
Code::InvalidApiKeyIndexes