Repository Public
1# =============================================================================2# SP BOUCHER | INFRASTRUCTURE & AUTOMATION3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# Project : MacLustr LLM API7# File : src/maclustr_llm/errors.py8# Description : Erreurs au format OpenAI : {"error": {"message", "type",9# "param", "code"}}.10# Module : maclustr_llm11# Repository : https://git.spboucher.ai/maclustr-llm12# Created : 2026-11-1913# Last Modified : 2026-11-1914#15# Author : Simon-Pierre Boucher16# Position : Professor of Finance17# Institution : Université du Québec en Outaouais (UQO)18# Organization : SP Boucher19# Website : https://spboucher.ai20# Git : https://git.spboucher.ai21#22# Preferred Contact: admin@spboucher.ai23# Technical Contact: dev@spboucher.ai24# Security/Admin : admin@spboucher.ai25#26# Copyright (c) 202627# Simon-Pierre Boucher28# All rights reserved where applicable. This file is governed by the29# applicable project license, copyright notices and relevant law.30# Existing third-party rights and notices remain unaffected.31#32# License : None specified (no license file in this repository)33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041"""Erreurs au format OpenAI : {"error": {"message", "type", "param", "code"}}."""4243from __future__ import annotations4445from typing import Any464748class APIError(Exception):49 def __init__(50 self,51 status: int,52 message: str,53 *,54 type_: str = "invalid_request_error",55 code: str | None = None,56 param: str | None = None,57 headers: dict[str, str] | None = None,58 ) -> None:59 super().__init__(message)60 self.status = status61 self.message = message62 self.type = type_63 self.code = code64 self.param = param65 self.headers = headers or {}6667 def body(self, request_id: str | None = None) -> dict[str, Any]:68 err: dict[str, Any] = {"message": self.message, "type": self.type, "param": self.param, "code": self.code}69 if request_id:70 err["request_id"] = request_id71 return {"error": err}727374def bad_request(message: str, param: str | None = None, code: str | None = None) -> APIError:75 return APIError(400, message, param=param, code=code)767778def unauthorized(message: str = "Clé API absente ou invalide.", code: str = "invalid_api_key") -> APIError:79 return APIError(401, message, type_="authentication_error", code=code, headers={"WWW-Authenticate": "Bearer"})808182def forbidden(message: str, code: str = "permission_denied") -> APIError:83 return APIError(403, message, type_="permission_error", code=code)848586def not_found(message: str, code: str = "not_found") -> APIError:87 return APIError(404, message, type_="not_found_error", code=code)888990def rate_limited(message: str, retry_after: float, code: str = "rate_limit_exceeded") -> APIError:91 return APIError(92 429,93 message,94 type_="rate_limit_error",95 code=code,96 headers={"Retry-After": str(max(1, int(retry_after + 0.999)))},97 )9899100def overloaded(message: str, retry_after: float = 5, code: str = "engine_overloaded") -> APIError:101 return APIError(102 503, message, type_="overloaded_error", code=code, headers={"Retry-After": str(max(1, int(retry_after)))}103 )104105106def upstream(message: str, code: str = "engine_error", status: int = 502) -> APIError:107 return APIError(status, message, type_="api_error", code=code)108