|
1 | 1 | use lambda_http::{http::StatusCode, Body, Error, Response};
|
2 | 2 | use serde::Serialize;
|
3 |
| -use std::error::Error as StdError; |
4 |
| -use tracing::error; |
5 | 3 |
|
6 |
| -pub fn ok<T: Serialize>(val: T) -> Result<Response<Body>, Error> { |
| 4 | +pub fn ok(val: impl Serialize) -> Result<Response<Body>, Error> { |
7 | 5 | Ok(Response::builder()
|
8 | 6 | .status(StatusCode::OK)
|
9 | 7 | .header("content-type", "application/json")
|
10 | 8 | .body(Body::Text(serde_json::to_string(&val).unwrap()))?)
|
11 | 9 | }
|
12 | 10 |
|
13 |
| -#[derive(Serialize)] |
14 |
| -pub struct APIError { |
15 |
| - pub message: &'static str, |
16 |
| - pub code: &'static str, |
17 |
| -} |
18 |
| - |
19 |
| -impl From<APIError> for lambda_http::Body { |
20 |
| - fn from(val: APIError) -> Self { |
21 |
| - lambda_http::Body::Text(serde_json::to_string(&val).unwrap()) |
22 |
| - } |
23 |
| -} |
24 |
| - |
25 |
| -pub fn bad_request(message: &'static str) -> Result<Response<Body>, Error> { |
| 11 | +pub fn bad_request(val: impl Serialize) -> Result<Response<Body>, Error> { |
26 | 12 | Ok(Response::builder()
|
27 | 13 | .status(StatusCode::BAD_REQUEST)
|
28 | 14 | .header("content-type", "application/json")
|
29 |
| - .body( |
30 |
| - APIError { |
31 |
| - message, |
32 |
| - code: "bad_request", |
33 |
| - } |
34 |
| - .into(), |
35 |
| - )?) |
36 |
| -} |
37 |
| - |
38 |
| -#[derive(Serialize)] |
39 |
| -struct Found { |
40 |
| - found: bool, |
41 |
| -} |
42 |
| - |
43 |
| -impl From<Found> for lambda_http::Body { |
44 |
| - fn from(val: Found) -> Self { |
45 |
| - lambda_http::Body::Text(serde_json::to_string(&val).unwrap()) |
46 |
| - } |
47 |
| -} |
48 |
| - |
49 |
| -const NOT_FOUND: Found = Found { found: false }; |
50 |
| - |
51 |
| -pub fn not_found() -> Result<Response<Body>, Error> { |
52 |
| - Ok(Response::builder() |
53 |
| - .status(StatusCode::OK) |
54 |
| - .header("content-type", "application/json") |
55 |
| - .body(NOT_FOUND.into())?) |
| 15 | + .body(Body::Text(serde_json::to_string(&val).unwrap()))?) |
56 | 16 | }
|
57 | 17 |
|
58 |
| -pub fn endpoint_not_found() -> Result<Response<Body>, Error> { |
| 18 | +pub fn not_found(val: impl Serialize) -> Result<Response<Body>, Error> { |
59 | 19 | Ok(Response::builder()
|
60 | 20 | .status(StatusCode::NOT_FOUND)
|
61 | 21 | .header("content-type", "application/json")
|
62 |
| - .body( |
63 |
| - APIError { |
64 |
| - message: "Not found", |
65 |
| - code: "not_found", |
66 |
| - } |
67 |
| - .into(), |
68 |
| - )?) |
| 22 | + .body(Body::Text(serde_json::to_string(&val).unwrap()))?) |
69 | 23 | }
|
70 | 24 |
|
71 |
| -pub fn unauthorized() -> Result<Response<Body>, Error> { |
| 25 | +pub fn unauthorized(val: impl Serialize) -> Result<Response<Body>, Error> { |
72 | 26 | Ok(Response::builder()
|
73 | 27 | .status(StatusCode::UNAUTHORIZED)
|
74 | 28 | .header("content-type", "application/json")
|
75 |
| - .body( |
76 |
| - APIError { |
77 |
| - message: "Unauthorized", |
78 |
| - code: "unauthorized", |
79 |
| - } |
80 |
| - .into(), |
81 |
| - )?) |
| 29 | + .body(Body::Text(serde_json::to_string(&val).unwrap()))?) |
82 | 30 | }
|
83 | 31 |
|
84 |
| -pub fn internal_server_error(err: impl StdError) -> Result<Response<Body>, Error> { |
85 |
| - error!(error = ?err, "internal server error"); |
| 32 | +pub fn internal_server_error(val: impl Serialize) -> Result<Response<Body>, Error> { |
86 | 33 | Ok(Response::builder()
|
87 | 34 | .status(StatusCode::INTERNAL_SERVER_ERROR)
|
88 | 35 | .header("content-type", "application/json")
|
89 |
| - .body( |
90 |
| - APIError { |
91 |
| - message: "Internal server error", |
92 |
| - code: "internal_server_error", |
93 |
| - } |
94 |
| - .into(), |
95 |
| - )?) |
| 36 | + .body(Body::Text(serde_json::to_string(&val).unwrap()))?) |
96 | 37 | }
|
0 commit comments