Skip to content

How to Err NO_CONTENT with OpenAPI? #459

Answered by sunli829
ShayBox asked this question in Q&A
Discussion options

You must be logged in to vote

You have two ways:

async fn load_rows(&self) -> Result<Option<Json<Vec<Value>>>> {
   ...
}

Return error code directly

async fn get(&self) -> Result<Json<Vec<Value>>> {
    let res = load_rows().await?;
    res.ok_or_else(|| poem::Error::from_status(StatusCode::NO_CONTENT))
}

Define the response using the ApiResponse macro

Recommended, response status codes are shown in the documentation

#[derive(ApiResponse)]
enum MyResponse {
    #[oai(status = 200)]
    Ok(Json<Vec<Value>>),

    /// reason
    #[oai(status = 204)]
    NoContent,
}

async fn get(&self) -> Result<MyResponse > {
    Ok(match load_rows().await? {
        Some(rows) => MyResponse::Ok(rows),
        None => MyResponse::NoCo…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ShayBox
Comment options

Answer selected by ShayBox
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants