Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.
/ baxe Public archive

A library that simplifies error handling in Axum

License

Notifications You must be signed in to change notification settings

iltumio/baxe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

❗❗ This project has been moved to zyphelabs/baxe ❗❗

Better Axum Errors

License Crates.io Version

Description

Better Axum Error (aka. baxe) is a utility that streamlines error handling in backend services built with Axum. With a simple and readable macro, you can define your backend errors once and automatically generate standardized JSON error responses, saving time and reducing complexity.

Table of Contents

Installation

cargo add baxe

Usage

use baxe::BackendError; // Import the trait

#[baxe::error] // Use the macro to define your errors
pub enum BackendErrors {
    #[baxe(status = StatusCode::BAD_REQUEST, tag = "bad_request", code = 400, message = "Bad request")]
    BadRequest(String),
    #[baxe(status = StatusCode::UNAUTHORIZED, tag = "auth/invalid_email_or_password", code = 10_000, message = "Invalid email or password")]
    InvalidEmailOrPassword,
    #[baxe(status = StatusCode::BAD_REQUEST, tag = "auth/invalid_email_format", code = 10_001, message = "Invalid email format")]
    InvalidEmailFormat,
}

Example axum handler:

pub async fn handler() -> Result<Json<String>, BaxeError> {
    if let Err(e) = validate_email(email) {
        return Err(BackendErrors::InvalidEmailFormat.into());
    }

    Ok(Json("Hello, world!".to_string()))
}

automatically generates the following response in case of error:

pub struct BaxeError {
    pub status_code: StatusCode,
    pub message: String,
    pub code: u16,
    pub error_tag: String,
}

that translates to the following json response:

{
  "message": "Invalid email format",
  "code": 10001,
  "error_tag": "auth/invalid_email_format"
}

Contributing

Feel free to open issues and send PRs. We will evaluate them together in the comment section.

License

This project is licensed under the MIT License.

About

A library that simplifies error handling in Axum

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages