Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

2.1 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

//! Error contract — uniform `{ ok:false, error: { code, message } }` envelope
//! with the status codes from `app/errors.py` / `app/main.py`.

use axum::{http::StatusCode, response::IntoResponse, Json};
use serde_json::json;

#[derive(Debug)]
pub struct ApiError {
    pub status: u16,
    pub code: String,
    pub message: String,
}

impl ApiError {
    pub fn new(status: u16, code: &str, message: impl Into<String>) -> Self {
        ApiError {
            status,
            code: code.to_string(),
            message: message.into(),
        }
    }
    pub fn invalid(message: impl Into<String>) -> Self {
        ApiError::new(400, "invalid_input", message)
    }
    pub fn unauthorized(message: impl Into<String>) -> Self {
        ApiError::new(401, "unauthorized", message)
    }
}

impl From<converter::convert::ConvertError> for ApiError {
    fn from(e: converter::convert::ConvertError) -> Self {
        let status = match e.code.as_str() {
            "READ_FAILED" => 422,
            "TESSELLATION_FAILED" => 500,
            "UPLOAD_FAILED" => 502,
            "INVALID_INPUT" => 400,
            "LIMIT_EXCEEDED" => 413,
            "BUSY" => 429,
            _ => 500,
        };
        ApiError::new(status, &e.code, e.message)
    }
}

impl IntoResponse for ApiError {
    fn into_response(self) -> axum::response::Response {
        let status = StatusCode::from_u16(self.status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
        let mut resp = (
            status,
            Json(json!({"ok": false, "error": {"code": self.code, "message": self.message}})),
        )
            .into_response();
        // 429s tell callers when to come back so their retries don't hammer
        // the concurrency semaphore (the app honors this with backoff).
        if self.status == 429 {
            resp.headers_mut()
                .insert("Retry-After", axum::http::HeaderValue::from_static("15"));
        }
        resp
    }
}