"""Application.""" import sentry_sdk from gql.transport.exceptions import TransportError from httpx import HTTPError from pydantic import ValidationError from sqlalchemy.exc import TimeoutError as SQLAlchemyTimeoutError from delivery_metadata import config from delivery_metadata.api import error_handlers from delivery_metadata.api.app import app from delivery_metadata.api.routers import delivery_metadata, infra from delivery_metadata.exceptions import ( IncompatibleXMLValue, NotFoundException, ProductIneligible, SchemaValidationError, UnprocessableException, ) app.include_router(delivery_metadata.router) app.include_router(infra.router) # Register exception handlers app.add_exception_handler(Exception, error_handlers.default_error_handler) app.add_exception_handler( HTTPError, error_handlers.upstream_error_handler, ) app.add_exception_handler(TransportError, error_handlers.upstream_error_handler) app.add_exception_handler( SQLAlchemyTimeoutError, error_handlers.upstream_error_handler, ) app.add_exception_handler( ProductIneligible, error_handlers.product_ineligible_error_handler, # type: ignore[arg-type] ) app.add_exception_handler( ValidationError, error_handlers.pydantic_validation_error_handler, # type: ignore[arg-type] ) app.add_exception_handler( NotFoundException, error_handlers.not_found_error_handler, # type: ignore[arg-type] ) app.add_exception_handler( IncompatibleXMLValue, error_handlers.incompatible_xml_value_error_handler, ) app.add_exception_handler( UnprocessableException, error_handlers.unprocessable_error_handler, # type: ignore[arg-type] ) app.add_exception_handler( SchemaValidationError, error_handlers.schema_validation_error_handler, # type: ignore[arg-type] ) if config.ENVIRONMENT in [config.PROD_ENVIRONMENT, config.QA_ENVIRONMENT]: if not config.SENTRY_DSN: raise Exception(f"Sentry is not configured in {config.ENVIRONMENT}") if config.SENTRY_DSN: sentry_sdk.init(dsn=config.SENTRY_DSN)