Mobile support to come.
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
use serde::Serialize;
use thiserror::Error;
use gitdot_core::error::{
AuthorizationError, GitHttpError, OAuthError, OrganizationError, QuestionError,
RepositoryError, UserError,
};
use super::AppResponse;
#[derive(Debug, Error)]
pub enum AppError {
#[error(transparent)]
Authorization(#[from] AuthorizationError),
#[error(transparent)]
User(#[from] UserError),
#[error(transparent)]
Organization(#[from] OrganizationError),
#[error(transparent)]
Repository(#[from] RepositoryError),
#[error(transparent)]
Question(#[from] QuestionError),
#[error(transparent)]
GitHttp(#[from] GitHttpError),
#[error(transparent)]
OAuth(#[from] OAuthError),
#[error(transparent)]
Internal(#[from] anyhow::Error),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct AppErrorMessage {
pub message: String,
}
impl IntoResponse for AppError {
fn into_response(self) -> Response {
match self {
AppError::Authorization(e) => {
let status_code = match e {
AuthorizationError::InvalidRequest(_) => StatusCode::BAD_REQUEST,
AuthorizationError::NotFound(_) => StatusCode::NOT_FOUND,
AuthorizationError::DatabaseError(_) => StatusCode::INTERNAL_SERVER_ERROR,
_ => StatusCode::UNAUTHORIZED,
};
let response = AppResponse::new(
status_code,
AppErrorMessage {
message: e.to_string(),
},
);
response.into_response()
}
AppError::User(e) => {
let status_code = match e {
UserError::NotFound(_) => StatusCode::NOT_FOUND,
UserError::InvalidUserName(_) => StatusCode::BAD_REQUEST,
UserError::NameTaken(_) => StatusCode::CONFLICT,
UserError::ReservedName(_) => StatusCode::CONFLICT,
UserError::DatabaseError(_) => StatusCode::INTERNAL_SERVER_ERROR,
};
let response = AppResponse::new(
status_code,
AppErrorMessage {
message: e.to_string(),
},
);
response.into_response()
}
AppError::Organization(e) => {
let status_code = match e {
OrganizationError::Duplicate(_) => StatusCode::CONFLICT,
OrganizationError::MemberAlreadyExists(_) => StatusCode::CONFLICT,
OrganizationError::NotFound(_) => StatusCode::NOT_FOUND,
OrganizationError::UserNotFound(_) => StatusCode::NOT_FOUND,
OrganizationError::InvalidOrganizationName(_) => StatusCode::BAD_REQUEST,
OrganizationError::InvalidUserName(_) => StatusCode::BAD_REQUEST,
OrganizationError::InvalidRole(_) => StatusCode::BAD_REQUEST,
OrganizationError::DatabaseError(_) => StatusCode::INTERNAL_SERVER_ERROR,
};
let response = AppResponse::new(
status_code,
AppErrorMessage {
message: e.to_string(),
},
);
response.into_response()
}
AppError::Repository(e) => {
let status_code = match e {
RepositoryError::Duplicate(_) => StatusCode::CONFLICT,
RepositoryError::OwnerNotFound(_) => StatusCode::NOT_FOUND,
RepositoryError::InvalidOwnerName(_) => StatusCode::BAD_REQUEST,
RepositoryError::InvalidRepositoryName(_) => StatusCode::BAD_REQUEST,
RepositoryError::InvalidOwnerType(_) => StatusCode::BAD_REQUEST,
RepositoryError::InvalidVisibility(_) => StatusCode::BAD_REQUEST,
RepositoryError::GitError(_) => StatusCode::INTERNAL_SERVER_ERROR,
RepositoryError::DatabaseError(_) => StatusCode::INTERNAL_SERVER_ERROR,
};
let response = AppResponse::new(
status_code,
AppErrorMessage {
message: e.to_string(),
},
);
response.into_response()
}
AppError::Question(e) => {
let status_code = match e {
QuestionError::InvalidOwnerName(_) => StatusCode::BAD_REQUEST,
QuestionError::InvalidRepositoryName(_) => StatusCode::BAD_REQUEST,
QuestionError::InvalidVoteValue(_) => StatusCode::BAD_REQUEST,
QuestionError::QuestionNotFound(_) => StatusCode::NOT_FOUND,
QuestionError::AnswerNotFound(_) => StatusCode::NOT_FOUND,
QuestionError::CommentNotFound(_) => StatusCode::NOT_FOUND,
QuestionError::RepositoryNotFound(_) => StatusCode::NOT_FOUND,
QuestionError::VoteTargetNotFound(_) => StatusCode::NOT_FOUND,
QuestionError::DatabaseError(_) => StatusCode::INTERNAL_SERVER_ERROR,
};
let response = AppResponse::new(
status_code,
AppErrorMessage {
message: e.to_string(),
},
);
response.into_response()
}
AppError::GitHttp(e) => {
let status_code = match e {
GitHttpError::InvalidOwnerName(_) => StatusCode::BAD_REQUEST,
GitHttpError::InvalidRepositoryName(_) => StatusCode::BAD_REQUEST,
GitHttpError::InvalidService(_) => StatusCode::BAD_REQUEST,
_ => StatusCode::INTERNAL_SERVER_ERROR,
};
let response = AppResponse::new(
status_code,
AppErrorMessage {
message: e.to_string(),
},
);
response.into_response()
}
AppError::OAuth(e) => {
let status_code = match &e {
OAuthError::AuthorizationPending => StatusCode::BAD_REQUEST,
OAuthError::ExpiredToken => StatusCode::BAD_REQUEST,
OAuthError::AccessDenied => StatusCode::BAD_REQUEST,
OAuthError::InvalidDeviceCode => StatusCode::BAD_REQUEST,
OAuthError::InvalidUserCode(_) => StatusCode::BAD_REQUEST,
OAuthError::InvalidRequest(_) => StatusCode::BAD_REQUEST,
OAuthError::DatabaseError(_) => StatusCode::INTERNAL_SERVER_ERROR,
};
let response = AppResponse::new(
status_code,
AppErrorMessage {
message: e.to_string(),
},
);
response.into_response()
}
AppError::Internal(e) => {
tracing::error!("{}", e);
let response = AppResponse::new(
StatusCode::INTERNAL_SERVER_ERROR,
AppErrorMessage {
message: e.to_string(),
},
);
response.into_response()
}
}
}
}
replaced create_user api with validate_name api
mikkel•0e0bf523d ago
created temporary signup page
mikkel•5a9138b3d ago
added reserved user names to avoid
mikkel•f0953d45d ago
implemented create user endpoint
mikkel•5b3ad656d ago
wrote internal dto unit tests
mikkel•bf7448d7d ago
wired up oauth in backend
mikkel•13cdd308d ago
new 404 error in authorization service and NotFoudn
baepaul•432dc8e9d ago
implemented get user and list user repos apis
mike•7ce056912d ago
added add member to org api
mike•47e08a813d ago
implemented voting system
mike•cfbefbc13d ago
rolled back to standard question apis
mike•ef0cc8213d ago
wired up question apis
mike•4ea4d4014d ago
created authorization service
mike•b104ec915d ago
renamed GitHttpBackendService -> GitHttpService
mike•c1acceb16d ago
updated all internal DTO to use newtypes for validation
mike•8d32ab016d ago
refactoring git_http in progress
mike•febbd1116d ago
refactored repository core
mike•ca1236717d ago
migrated git http endpoints to new arch
mike•fbc763320d ago
migrated create_repository to new design
mike•f5802ef20d ago
implemented optional extractor to extract auth user from jwt token in request header
mike•988f77f22d ago
prototyped create org handler
mike•38ec76d23d ago
prototyped top-level response and error
mike•fae48ec1mo ago