# Copyright 2022 MOSEC Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Exceptions used in the Worker.

Suppose the input dataflow of our model server is as follows:

**bytes** --- *deserialize*<sup>(decoding)</sup> ---> **data**
--- *parse*<sup>(validation)</sup> ---> **valid data**

If the raw bytes cannot be successfully deserialized, the `DecodingError`
is raised; if the decoded data cannot pass the validation check (usually
implemented by users), the `ValidationError` should be raised.
"""

from .protocol import HTTPStautsCode


class MosecError(Exception):
    """Mosec basic exception."""

    code = HTTPStautsCode.INTERNAL_ERROR
    msg = "mosec error"


class ClientError(MosecError):
    """Client side error.

    This error indicates that the server cannot or will not process the request
    due to something that is perceived to be a client error. It will return the
    details to the client side with
    [HTTP 400](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400).
    """

    code = HTTPStautsCode.BAD_REQUEST
    msg = "bad request"


class ServerError(MosecError):
    """Server side error.

    This error indicates that the server encountered an unexpected condition
    that prevented it from fulfilling the request. It will return the details
    to the client side with
    [HTTP 500](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500).

    Attention: be careful about the returned message since it may contain some
    sensitive information. If you don't want to return the details, just raise
    an exception that is not inherited from `mosec.errors.MosecError`.
    """

    code = HTTPStautsCode.INTERNAL_ERROR
    msg = "internal error"


class EncodingError(ServerError):
    """Serialization error.

    The `EncodingError` should be raised in user-implemented codes when
    the serialization for the response bytes fails. This error will set
    to status code to
    [HTTP 500](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500)
    and show the details in the response.
    """

    msg = "encoding error"


class DecodingError(ClientError):
    """De-serialization error.

    The `DecodingError` should be raised in user-implemented codes
    when the de-serialization for the request bytes fails. This error
    will set the status code to
    [HTTP 400]("https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400)
    in the response.
    """

    msg = "decoding error"


class ValidationError(MosecError):
    """Request data validation error.

    The `ValidationError` should be raised in user-implemented codes,
    where the validation for the input data fails. Usually, it should be
    put after the data de-serialization, which converts the raw bytes
    into structured data. This error will set the status code to
    [HTTP 422](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422)
    in the response.
    """

    code = HTTPStautsCode.VALIDATION_ERROR
    msg = "request validation error"
