# Path: via/exceptions.py
# class BadURL(RequestBasedException):
#     """An invalid URL was discovered."""
# 
#     def __init__(self, message, requests_err=None, url=None):
#         super().__init__(message, requests_err)
#         self.url = url
# 
#     status_int = 400
# 
# class UnhandledUpstreamException(UpstreamServiceError):
#     """Something we did not plan for went wrong."""
# 
#     status_int = 417
# 
# class UpstreamServiceError(RequestBasedException):
#     """Something went wrong when calling an upstream service."""
# 
#     status_int = 409
# 
# class UpstreamTimeout(UpstreamServiceError):
#     """We timed out waiting for an upstream service."""
# 
#     # "504 - Gateway Timeout" is the correct thing to raise, but this
#     # will cause Cloudflare to intercept it:
#     # https://support.cloudflare.com/hc/en-us/articles/115003011431-Troubleshooting-Cloudflare-5XX-errors#502504error
#     # We're using "408 - Request Timeout" which is technically
#     # incorrect as it implies the user took too long, but has good semantics
#     status_int = 408
# 
# Path: via/resources.py
# def get_original_url(context):
#     """Get the original URL from a provided context object (if any)."""
# 
#     if isinstance(context, QueryURLResource):
#         getter = context.url_from_query
#     elif isinstance(context, PathURLResource):
#         getter = context.url_from_path
#     else:
#         return None
# 
#     try:
#         return getter()
#     except BadURL as err:
#         return err.url
#     except HTTPBadRequest:
#         return None


import h_pyramid_sentry
from pyramid.httpexceptions import HTTPClientError, HTTPNotFound
from pyramid.view import exception_view_config
from via.exceptions import (
    BadURL,
    UnhandledUpstreamException,
    UpstreamServiceError,
    UpstreamTimeout,
)
from via.resources import get_original_url
        "title": "Page not found",
        "long_description": [
            "The URL you asked for is not part of this service.",
            "Please check the URL you have entered.",
        ],
        "stage": "request",
        "retryable": False,
    },
    HTTPClientError: {
        "title": "Bad request",
        "long_description": [
            "We can't process the request because we don't understand it."
        ],
        "stage": "request",
        "retryable": False,
    },
}


def _get_meta(exception):
    """Convert an unhandled error into an managed error."""

    meta = EXCEPTION_MAP.get(type(exception))
    if meta:
        return meta

    for exception_type, meta in EXCEPTION_MAP.items():
        if isinstance(exception, exception_type):
            return meta


# Path: via/exceptions.py
# class BadURL(RequestBasedException):
#     """An invalid URL was discovered."""
# 
#     def __init__(self, message, requests_err=None, url=None):
#         super().__init__(message, requests_err)
#         self.url = url
# 
#     status_int = 400
# 
# class UnhandledUpstreamException(UpstreamServiceError):
#     """Something we did not plan for went wrong."""
# 
#     status_int = 417
# 
# class UpstreamServiceError(RequestBasedException):
#     """Something went wrong when calling an upstream service."""
# 
#     status_int = 409
# 
# class UpstreamTimeout(UpstreamServiceError):
#     """We timed out waiting for an upstream service."""
# 
#     # "504 - Gateway Timeout" is the correct thing to raise, but this
#     # will cause Cloudflare to intercept it:
#     # https://support.cloudflare.com/hc/en-us/articles/115003011431-Troubleshooting-Cloudflare-5XX-errors#502504error
#     # We're using "408 - Request Timeout" which is technically
#     # incorrect as it implies the user took too long, but has good semantics
#     status_int = 408
# 
# Path: via/resources.py
# def get_original_url(context):
#     """Get the original URL from a provided context object (if any)."""
# 
#     if isinstance(context, QueryURLResource):
#         getter = context.url_from_query
#     elif isinstance(context, PathURLResource):
#         getter = context.url_from_path
#     else:
#         return None
# 
#     try:
#         return getter()
#     except BadURL as err:
#         return err.url
#     except HTTPBadRequest:
#         return None


# Path: via/views/exceptions.py
import h_pyramid_sentry
from pyramid.httpexceptions import HTTPClientError, HTTPNotFound
from pyramid.view import exception_view_config
from via.exceptions import (
    BadURL,
    UnhandledUpstreamException,
    UpstreamServiceError,
    UpstreamTimeout,
)
from via.resources import get_original_url

        "title": "Page not found",
        "long_description": [
            "The URL you asked for is not part of this service.",
            "Please check the URL you have entered.",
        ],
        "stage": "request",
        "retryable": False,
    },
    HTTPClientError: {
        "title": "Bad request",
        "long_description": [
            "We can't process the request because we don't understand it."
        ],
        "stage": "request",
        "retryable": False,
    },
}


def _get_meta(exception):
    """Convert an unhandled error into an managed error."""

    meta = EXCEPTION_MAP.get(type(exception))
    if meta:
        return meta

    for exception_type, meta in EXCEPTION_MAP.items():
        if isinstance(exception, exception_type):
            return meta


    return EXCEPTION_MAP.get(UnhandledUpstreamException)