"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from __future__ import annotations
from dataclasses import dataclass, field
from factify.errors import FactifyError
from factify.models import (
    errordetail as models_errordetail,
    errorresponse as models_errorresponse,
)
from factify.types import BaseModel
import httpx
from typing import List, Optional


class ErrorResponseData(BaseModel):
    code: models_errorresponse.Code
    r"""ConnectRPC error code. Maps to HTTP status codes:
    - invalid_argument, failed_precondition -> 400
    - unauthenticated -> 401
    - permission_denied -> 403
    - not_found -> 404
    - already_exists -> 409
    - resource_exhausted -> 429
    - deadline_exceeded -> 408
    - canceled -> 499
    - internal, unknown -> 500 (unknown may appear as 400 for malformed JSON requests)
    - unimplemented -> 501
    - unavailable -> 503

    """
    message: str
    r"""Human-readable error message"""
    details: Optional[List[models_errordetail.ErrorDetail]] = None
    r"""Structured error details for programmatic handling"""


@dataclass(unsafe_hash=True)
class ErrorResponse(FactifyError):
    r"""Invalid request parameters"""

    data: ErrorResponseData = field(hash=False)

    def __init__(
        self,
        data: ErrorResponseData,
        raw_response: httpx.Response,
        body: Optional[str] = None,
    ):
        fallback = body or raw_response.text
        message = str(data.message) or fallback
        super().__init__(message, raw_response, body)
        object.__setattr__(self, "data", data)
