Skip to content

Commit 4e1a61a

Browse files
committed
Exceptions restructure
1 parent 9b05d7b commit 4e1a61a

27 files changed

+287
-209
lines changed

openapi_core/exceptions.py

-77
This file was deleted.

openapi_core/schema/exceptions.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""OpenAPI core schema exceptions module"""
2+
3+
4+
class OpenAPIError(Exception):
5+
pass
6+
7+
8+
class OpenAPIMappingError(OpenAPIError):
9+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from openapi_core.schema.exceptions import OpenAPIMappingError
2+
3+
4+
class OpenAPIMediaTypeError(OpenAPIMappingError):
5+
pass
6+
7+
8+
class InvalidMediaTypeValue(OpenAPIMediaTypeError):
9+
pass
10+
11+
12+
class InvalidContentType(OpenAPIMediaTypeError):
13+
pass

openapi_core/schema/media_types/models.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
from collections import defaultdict
33

44
from json import loads
5-
from six import iteritems
65

7-
from openapi_core.exceptions import InvalidValueType, InvalidMediaTypeValue
6+
from openapi_core.schema.media_types.exceptions import InvalidMediaTypeValue
7+
from openapi_core.schema.schemas.exceptions import InvalidSchemaValue
88

99

1010
MEDIA_TYPE_DESERIALIZERS = {
@@ -42,5 +42,5 @@ def unmarshal(self, value):
4242

4343
try:
4444
return self.schema.unmarshal(deserialized)
45-
except InvalidValueType as exc:
45+
except InvalidSchemaValue as exc:
4646
raise InvalidMediaTypeValue(str(exc))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from openapi_core.schema.exceptions import OpenAPIMappingError
2+
3+
4+
class OpenAPIOperationError(OpenAPIMappingError):
5+
pass
6+
7+
8+
class InvalidOperation(OpenAPIOperationError):
9+
pass

openapi_core/schema/operations/models.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
"""OpenAPI core operations models module"""
3-
from openapi_core.exceptions import InvalidResponse
3+
from openapi_core.schema.responses.exceptions import InvalidResponse
44

55

66
class Operation(object):
@@ -21,6 +21,7 @@ def __getitem__(self, name):
2121
return self.parameters[name]
2222

2323
def get_response(self, http_status='default'):
24+
# @todo: move to Responses object
2425
try:
2526
return self.responses[http_status]
2627
except KeyError:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from openapi_core.schema.exceptions import OpenAPIMappingError
2+
3+
4+
class OpenAPIParameterError(OpenAPIMappingError):
5+
pass
6+
7+
8+
class MissingParameter(OpenAPIParameterError):
9+
pass
10+
11+
12+
class MissingRequiredParameter(OpenAPIParameterError):
13+
pass
14+
15+
16+
class EmptyParameterValue(OpenAPIParameterError):
17+
pass
18+
19+
20+
class InvalidParameterValue(OpenAPIParameterError):
21+
pass

openapi_core/schema/parameters/models.py

+28-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
import logging
33
import warnings
44

5-
from openapi_core.exceptions import (
6-
EmptyValue, InvalidValueType, InvalidParameterValue,
7-
)
85
from openapi_core.schema.parameters.enums import ParameterLocation, ParameterStyle
6+
from openapi_core.schema.parameters.exceptions import (
7+
MissingRequiredParameter, MissingParameter, InvalidParameterValue,
8+
EmptyParameterValue,
9+
)
910
from openapi_core.schema.schemas.enums import SchemaType
11+
from openapi_core.schema.schemas.exceptions import InvalidSchemaValue
1012

1113
log = logging.getLogger(__name__)
1214

@@ -68,6 +70,27 @@ def deserialize(self, value):
6870
deserializer = self.get_dererializer()
6971
return deserializer(value)
7072

73+
def get_value(self, request):
74+
location = request.parameters[self.location.value]
75+
76+
try:
77+
raw = location[self.name]
78+
except KeyError:
79+
if self.required:
80+
raise MissingRequiredParameter(
81+
"Missing required `{0}` parameter".format(self.name))
82+
83+
if not self.schema or self.schema.default is None:
84+
raise MissingParameter(
85+
"Missing `{0}` parameter".format(self.name))
86+
87+
raw = self.schema.default
88+
89+
if self.aslist and self.explode:
90+
return location.getlist(self.name)
91+
92+
return raw
93+
7194
def unmarshal(self, value):
7295
if self.deprecated:
7396
warnings.warn(
@@ -77,7 +100,7 @@ def unmarshal(self, value):
77100

78101
if (self.location == ParameterLocation.QUERY and value == "" and
79102
not self.allow_empty_value):
80-
raise EmptyValue(
103+
raise EmptyParameterValue(
81104
"Value of {0} parameter cannot be empty".format(self.name))
82105

83106
if not self.schema:
@@ -87,5 +110,5 @@ def unmarshal(self, value):
87110

88111
try:
89112
return self.schema.unmarshal(deserialized)
90-
except InvalidValueType as exc:
113+
except InvalidSchemaValue as exc:
91114
raise InvalidParameterValue(str(exc))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from openapi_core.schema.exceptions import OpenAPIMappingError
2+
3+
4+
class OpenAPIRequestBodyError(OpenAPIMappingError):
5+
pass
6+
7+
8+
class MissingRequestBody(OpenAPIRequestBodyError):
9+
pass

openapi_core/schema/request_bodies/models.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""OpenAPI core request bodies models module"""
2-
from openapi_core.exceptions import InvalidContentType
2+
3+
from openapi_core.schema.media_types.exceptions import InvalidContentType
4+
from openapi_core.schema.request_bodies.exceptions import MissingRequestBody
35

46

57
class RequestBody(object):
@@ -15,3 +17,9 @@ def __getitem__(self, mimetype):
1517
except KeyError:
1618
raise InvalidContentType(
1719
"Invalid mime type `{0}`".format(mimetype))
20+
21+
def get_value(self, request):
22+
if not request.body and self.required:
23+
raise MissingRequestBody("Missing required request body")
24+
25+
return request.body
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from openapi_core.schema.exceptions import OpenAPIMappingError
2+
3+
4+
class OpenAPIResponseError(OpenAPIMappingError):
5+
pass
6+
7+
8+
class InvalidResponse(OpenAPIResponseError):
9+
pass
10+
11+
12+
class MissingResponseContent(OpenAPIResponseError):
13+
pass

openapi_core/schema/responses/models.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""OpenAPI core responses models module"""
2-
from openapi_core.exceptions import InvalidContentType
2+
from openapi_core.schema.media_types.exceptions import InvalidContentType
3+
from openapi_core.schema.responses.exceptions import MissingResponseContent
34

45

56
class Response(object):
@@ -19,3 +20,9 @@ def __getitem__(self, mimetype):
1920
except KeyError:
2021
raise InvalidContentType(
2122
"Invalid mime type `{0}`".format(mimetype))
23+
24+
def get_value(self, response):
25+
if not response.data:
26+
raise MissingResponseContent("Missing response content")
27+
28+
return response.data
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from openapi_core.schema.exceptions import OpenAPIMappingError
2+
3+
4+
class OpenAPISchemaError(OpenAPIMappingError):
5+
pass
6+
7+
8+
class InvalidSchemaValue(OpenAPISchemaError):
9+
pass
10+
11+
12+
class UndefinedSchemaProperty(OpenAPISchemaError):
13+
pass
14+
15+
16+
class MissingSchemaProperty(OpenAPISchemaError):
17+
pass

openapi_core/schema/schemas/models.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
from six import iteritems
77

8-
from openapi_core.exceptions import (
9-
InvalidValueType, UndefinedSchemaProperty, MissingProperty, InvalidValue,
10-
)
118
from openapi_core.extensions.models.factories import ModelFactory
129
from openapi_core.schema.schemas.enums import SchemaType, SchemaFormat
10+
from openapi_core.schema.schemas.exceptions import (
11+
InvalidSchemaValue, UndefinedSchemaProperty, MissingSchemaProperty,
12+
)
1313
from openapi_core.schema.schemas.util import forcebool
1414

1515
log = logging.getLogger(__name__)
@@ -74,7 +74,7 @@ def cast(self, value):
7474
"""Cast value to schema type"""
7575
if value is None:
7676
if not self.nullable:
77-
raise InvalidValueType("Null value for non-nullable schema")
77+
raise InvalidSchemaValue("Null value for non-nullable schema")
7878
return self.default
7979

8080
if self.type is None:
@@ -89,7 +89,7 @@ def cast(self, value):
8989
try:
9090
return cast_callable(value)
9191
except ValueError:
92-
raise InvalidValueType(
92+
raise InvalidSchemaValue(
9393
"Failed to cast value of {0} to {1}".format(value, self.type)
9494
)
9595

@@ -104,7 +104,7 @@ def unmarshal(self, value):
104104
return None
105105

106106
if self.enum and casted not in self.enum:
107-
raise InvalidValue(
107+
raise InvalidSchemaValue(
108108
"Value of {0} not in enum choices: {1}".format(
109109
value, self.enum)
110110
)
@@ -116,7 +116,8 @@ def _unmarshal_collection(self, value):
116116

117117
def _unmarshal_object(self, value):
118118
if not isinstance(value, (dict, )):
119-
raise InvalidValueType("Value of {0} not an object".format(value))
119+
raise InvalidSchemaValue(
120+
"Value of {0} not an object".format(value))
120121

121122
all_properties = self.get_all_properties()
122123
all_required_properties = self.get_all_required_properties()
@@ -135,7 +136,7 @@ def _unmarshal_object(self, value):
135136
prop_value = value[prop_name]
136137
except KeyError:
137138
if prop_name in all_required_properties:
138-
raise MissingProperty(
139+
raise MissingSchemaProperty(
139140
"Missing schema property {0}".format(prop_name))
140141
if not prop.nullable and not prop.default:
141142
continue
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from openapi_core.schema.exceptions import OpenAPIMappingError
2+
3+
4+
class OpenAPIServerError(OpenAPIMappingError):
5+
pass
6+
7+
8+
class InvalidServer(OpenAPIServerError):
9+
pass

openapi_core/schema/specs/factories.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
from openapi_spec_validator import openapi_v3_spec_validator
66

7-
from openapi_core.exceptions import InvalidOperation, InvalidServer
7+
from openapi_core.schema.operations.exceptions import InvalidOperation
8+
from openapi_core.schema.servers.exceptions import InvalidServer
89
from openapi_core.schema.components.factories import ComponentsFactory
910
from openapi_core.schema.infos.factories import InfoFactory
1011
from openapi_core.schema.paths.generators import PathsGenerator

openapi_core/schema/specs/models.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import logging
44
from functools import partialmethod
55

6-
from openapi_core.exceptions import InvalidOperation, InvalidServer
6+
from openapi_core.schema.operations.exceptions import InvalidOperation
7+
from openapi_core.schema.servers.exceptions import InvalidServer
78

89

910
log = logging.getLogger(__name__)

0 commit comments

Comments
 (0)