Documentation about OpenAPI errors content types is confusing #821
-
In the documentation about error handling with OpenAPI, it appears as if With the following code: use salvo::http::{StatusCode, StatusError};
use salvo::oapi::{self, EndpointOutRegister, ToSchema};
use salvo::prelude::*;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ApiError {
#[error("Unexpected error")]
Unexpected,
}
#[async_trait]
impl Writer for ApiError {
async fn write(mut self, _req: &mut Request, depot: &mut Depot, res: &mut Response) {
res.render(StatusError::internal_server_error());
}
}
impl EndpointOutRegister for ApiError {
fn register(components: &mut oapi::Components, operation: &mut oapi::Operation) {
operation.responses.insert(
StatusCode::INTERNAL_SERVER_ERROR.as_str(),
oapi::Response::new("Internal server error")
.add_content("application/json", StatusError::to_schema(components)),
);
}
} if the endpoint handler returns an error, the clients gets a
This makes the OpenAPI specs inavlid since they say that 500 status code should come with json, but it doesn't. Is there anything I'm missing? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
If you add http header accept="application/json" to the request, will it still return plain text? |
Beta Was this translation helpful? Give feedback.
-
ah! that was it. Thanks @chrislearn. My endpoint returns |
Beta Was this translation helpful? Give feedback.
If you add http header accept="application/json" to the request, will it still return plain text?