-
SummaryI'm not an #[derive(Serialize, Deserialize)]
struct Payload(Option<serde_json::Value>);
impl Payload {
fn new(value: impl Serialize) -> core::result::Result<Self, InternalError> {
serde_json::to_value(value)
.map(|value| Self(Some(value)))
.map_err(|_| InternalError::new("Error"))
}
}
impl IntoResponse for Payload {
fn into_response(self) -> Response {
(StatusCode::OK, Json(self.0)).into_response()
}
}
#[derive(Serialize, Deserialize)]
struct InternalError(&'static str);
impl InternalError {
pub fn new(info: &'static str) -> Self {
Self(info)
}
}
impl IntoResponse for InternalError {
fn into_response(self) -> Response {
(StatusCode::INTERNAL_SERVER_ERROR, Json(self.0)).into_response()
}
}
#[derive(Clone, Default)]
struct MyState(Arc<Mutex<DummyStruct>>);
#[derive(Serialize)]
struct Response1 {
parameter1: f64,
parameter2: bool,
}
async fn first(
Path((parameter1, parameter2)): Path<(f64, bool)>,
Extension(state): Extension<MyState>,
) -> Result<Payload, Internalerror> {
let a = state.0.lock().await;
Err(InternalError::new("Error"))
}
#[derive(Deserialize)]
struct Inputs {
parameter1: f64,
#[serde(rename = "my-name")]
parameter2: bool,
}
async fn second(
Json(input): Json<Inputs>,
Extension(state): Extension<MyState>,
) -> Result<Payload, InternalError> {
let a = state.0.lock().await;
Payload::new(Response1 {
parameter1: input.parameter1,
parameter2: input.parameter2,
})
}
#[tokio::main]
async fn main() {
let first_router: Router = Router::new().route("/first", post(first));
let second_router: Router = Router::new().route("/second", post(second));
} I get the following error:
axum version0.7.5 |
Beta Was this translation helpful? Give feedback.
Answered by
mladedav
Jun 10, 2024
Replies: 2 comments 5 replies
-
Try decorating the handler with |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
jplatte
-
Try async fn second(
Extension(state): Extension<MyState>,
Json(input): Json<Inputs>
) -> Result<Payload, InternalError> {
let a = state.0.lock().await;
Payload::new(Response1 {
parameter1: input.parameter1,
parameter2: input.parameter2,
})
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try decorating the handler with
#[debug_handler]
, that might provide you with a better error message.