joaommartins / actix-web-thiserror

A crate that extends the thiserror crate functionality to automatically return a proper actix-web response.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

actix-web-thiserror

License Contributors GitHub Repo stars crates.io

A crate that extends the thiserror crate functionality to automatically return a proper actix-web response.

Documentation

Error definition

use actix_web_thiserror::ResponseError;
use log::error;
use thiserror::Error;

#[derive(Debug, Error, ResponseError)]
pub enum Base64ImageError {
  #[response(reason = "INVALID_IMAGE_FORMAT")]
  #[error("invalid image format")]
  InvalidImageFormat,
  #[response(reason = "INVALID_STRING")]
  #[error("invalid string")]
  InvalidString,
}

Error implementation

pub async fn error_test() -> Result<HttpResponse, Error> {
  Err(Base64ImageError::InvalidImageFormat)?
}

Error response

The reason is a string that may be given to the client in some form to explain the error, if appropriate. Here it is as an enum that can be localized.

Note: This response has been formatted by a ResponseTransform.

{
    "result": 0,
    "reason": "INVALID_IMAGE_FORMAT"
}

Error logging

The error text automatically prints to the log when the error is returned out through a http response.

Apr 23 02:19:35.211 ERRO Response error: invalid image format
    Base64ImageError(InvalidImageFormat), place: example/src/handler.rs:5 example::handler

About

A crate that extends the thiserror crate functionality to automatically return a proper actix-web response.

License:MIT License


Languages

Language:Rust 100.0%