rp-postgrest-error

Crates.iorp-postgrest-error
lib.rsrp-postgrest-error
version
sourcesrc
created_at2024-10-26 09:23:22.345029
updated_at2024-11-24 21:00:57.469009
descriptionstrongly typed errors for PostgREST
homepagehttps://github.com/roberts-pumpurs/supabase-rs-utils
repositoryhttps://github.com/roberts-pumpurs/supabase-rs-utils
max_upload_size
id1423676
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Roberts Pumpurs (roberts-pumpurs)

documentation

README

rp-postgrest-error

A Rust crate for parsing and handling error responses from PostgREST and PostgreSQL, providing structured error types and utility functions.

Features

  • Structured Error Types: Differentiate between PostgreSQL errors, PostgREST errors, and custom errors.
  • HTTP Status Mapping: Map error codes to corresponding HTTP status codes for API responses.
  • Detailed Error Information: Access error messages, details, and hints for better error handling.
  • Trait Implementations: Implements std::error::Error and Display traits for seamless integration with Rust’s error handling ecosystem.

Installation

Add the following to your Cargo.toml:

[dependencies]
rp-postgrest-error = "0.1.0"

Replace "0.1.0" with the latest version of the crate.

Usage

Parsing Error Responses

The crate provides an Error enum that represents different types of errors that can occur when interacting with PostgREST:

  • Error::PostgresError for PostgreSQL errors.
  • Error::PostgrestError for PostgREST-specific errors.
  • Error::CustomError for any other errors.

To parse an error response from PostgREST, you can use the from_error_response method:

use rp_postgrest_error::{Error, ErrorResponse};

// Example error response from PostgREST
let error_response = ErrorResponse {
    message: "duplicate key value violates unique constraint".to_owned(),
    code: "23505".to_owned(),
    details: Some("Key (id)=(1) already exists.".to_owned()),
    hint: None,
};

// Parse the error response
let error = Error::from_error_response(error_response);

match error {
    Error::PostgresError(pg_error) => {
        println!("PostgreSQL Error: {}", pg_error.message);
    }
    Error::PostgrestError(pgrst_error) => {
        println!("PostgREST Error: {}", pgrst_error.message);
    }
    Error::CustomError(custom_error) => {
        println!("Custom Error: {}", custom_error.message);
    }
}
Commit count: 99

cargo fmt