1use std::borrow::Cow;
2
3use axum::response::{IntoResponse, Response};
4use http::StatusCode;
5
6#[derive(Debug, Clone)]
7pub struct ApigateError {
8 status: StatusCode,
9 message: Cow<'static, str>,
10}
11
12impl ApigateError {
13 pub fn new(status: StatusCode, message: impl Into<Cow<'static, str>>) -> Self {
14 Self {
15 status,
16 message: message.into(),
17 }
18 }
19
20 pub fn bad_request(message: impl Into<Cow<'static, str>>) -> Self {
21 Self::new(StatusCode::BAD_REQUEST, message)
22 }
23
24 pub fn unauthorized(message: impl Into<Cow<'static, str>>) -> Self {
25 Self::new(StatusCode::UNAUTHORIZED, message)
26 }
27
28 pub fn forbidden(message: impl Into<Cow<'static, str>>) -> Self {
29 Self::new(StatusCode::FORBIDDEN, message)
30 }
31
32 pub fn payload_too_large(message: impl Into<Cow<'static, str>>) -> Self {
33 Self::new(StatusCode::PAYLOAD_TOO_LARGE, message)
34 }
35
36 pub fn unsupported_media_type(message: impl Into<Cow<'static, str>>) -> Self {
37 Self::new(StatusCode::UNSUPPORTED_MEDIA_TYPE, message)
38 }
39
40 pub fn internal(message: impl Into<Cow<'static, str>>) -> Self {
41 Self::new(StatusCode::INTERNAL_SERVER_ERROR, message)
42 }
43}
44
45impl IntoResponse for ApigateError {
46 fn into_response(self) -> Response {
47 (
48 self.status,
49 [("content-type", "text/plain; charset=utf-8")],
50 self.message.into_owned(),
51 )
52 .into_response()
53 }
54}