File: errors.py

package info (click to toggle)
python-avro 1.12.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,180 kB
  • sloc: python: 7,734; sh: 771; xml: 738; java: 386; makefile: 28
file content (126 lines) | stat: -rw-r--r-- 4,387 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3

##
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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
#
# https://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.

import json


def _safe_pretty(schema):
    """Try to pretty-print a schema, but never raise an exception within another exception."""
    try:
        return json.dumps(json.loads(str(schema)), indent=2)
    except Exception:  # Never raise an exception within another exception.
        return schema


class AvroException(Exception):
    """The base class for exceptions in avro."""


class InvalidAvroBinaryEncoding(AvroException):
    """For invalid numbers of bytes read."""


class SchemaParseException(AvroException):
    """Raised when a schema failed to parse."""


class InvalidName(SchemaParseException):
    """User attempted to parse a schema with an invalid name."""


class InvalidDefault(SchemaParseException):
    """User attempted to parse a schema with an invalid default."""


class AvroWarning(UserWarning):
    """Base class for warnings."""


class IgnoredLogicalType(AvroWarning):
    """Warnings for unknown or invalid logical types."""


class AvroTypeException(AvroException):
    """Raised when datum is not an example of schema."""

    def __init__(self, *args):
        try:
            expected_schema, name, datum = args[:3]
        except (IndexError, ValueError):
            return super().__init__(*args)
        pretty_expected = json.dumps(json.loads(str(expected_schema)), indent=2)
        return super().__init__(f'The datum "{datum}" provided for "{name}" is not an example of the schema {pretty_expected}')


class InvalidDefaultException(AvroTypeException):
    """Raised when a default value isn't a suitable type for the schema."""


class AvroOutOfScaleException(AvroTypeException):
    """Raised when attempting to write a decimal datum with an exponent too large for the decimal schema."""

    def __init__(self, *args):
        try:
            scale, datum, exponent = args[:3]
        except (IndexError, ValueError):
            return super().__init__(*args)
        return super().__init__(f"The exponent of {datum}, {exponent}, is too large for the schema scale of {scale}")


class SchemaResolutionException(AvroException):
    def __init__(self, fail_msg, writers_schema=None, readers_schema=None, *args):
        writers_message = f"\nWriter's Schema: {_safe_pretty(writers_schema)}" if writers_schema else ""
        readers_message = f"\nReader's Schema: {_safe_pretty(readers_schema)}" if readers_schema else ""
        super().__init__((fail_msg or "") + writers_message + readers_message, *args)


class DataFileException(AvroException):
    """Raised when there's a problem reading or writing file object containers."""


class IONotReadyException(AvroException):
    """Raised when attempting an avro operation on an io object that isn't fully initialized."""


class AvroRemoteException(AvroException):
    """Raised when an error message is sent by an Avro requestor or responder."""


class ConnectionClosedException(AvroException):
    """Raised when attempting IPC on a closed connection."""


class ProtocolParseException(AvroException):
    """Raised when a protocol failed to parse."""


class UnsupportedCodec(NotImplementedError, AvroException):
    """Raised when the compression named cannot be used."""


class UsageError(RuntimeError, AvroException):
    """An exception raised when incorrect arguments were passed."""


class AvroRuntimeException(RuntimeError, AvroException):
    """Raised when compatibility parsing encounters an unknown type"""


class UnknownFingerprintAlgorithmException(AvroException):
    """Raised when attempting to generate a fingerprint with an unknown algorithm"""