Skip to content

Commit 30d8602

Browse files
committed
Merge branch 'main' into pre-commit-ci-update-config
2 parents e5c1ebd + 48e27ad commit 30d8602

21 files changed

+2875
-11
lines changed

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Generate coverage report
1717
run: |
1818
pip install .
19-
pip install pytest pytest-django pytest-cov
19+
pip install django~=3.2 pytest pytest-django pytest-cov drf-spectacular django-filter
2020
pytest --cov --cov-report=xml
2121
- name: Upload coverage to Codecov
2222
uses: codecov/codecov-action@v3

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ to allow you to get more information out of the traceback. You can enable standa
9898
DRF_STANDARDIZED_ERRORS = {"ENABLE_IN_DEBUG_FOR_UNHANDLED_EXCEPTIONS": True}
9999
```
100100

101+
## Integration with DRF spectacular
102+
If you plan to use [drf-spectacular](https://github.com/tfranzel/drf-spectacular) to generate an OpenAPI 3 schema,
103+
install with `pip install drf-standardized-errors[openapi]`. After that, check the doc page for configuring the
104+
integration.
101105

102106
## Links
103107

docs/changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

77
## [UNRELEASED]
8+
### Added
9+
- add support for automatically generating error responses schema with [drf-spectacular](https://github.com/tfranzel/drf-spectacular).
810

911
## [0.11.0] - 2022-06-24
1012
### Changed (Backward-incompatible)

docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@
5151
# relative to this directory. They are copied after the builtin static files,
5252
# so a file named "default.css" will overwrite the builtin "default.css".
5353
# html_static_path = ["_static"]
54+
55+
myst_heading_anchors = 3

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ settings.md
99
error_response.md
1010
customization.md
1111
gotchas.md
12+
openapi.md
13+
openapi_sample_description.md
1214
contibuting.md
1315
release.md
1416
changelog.md

docs/openapi.md

Lines changed: 414 additions & 0 deletions
Large diffs are not rendered by default.

docs/openapi_sample_description.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Sample API Description
2+
3+
Check the [Tips and Tricks](openapi.md#hide-error-responses-that-show-in-every-operation) to understand how to use this sample API description.
4+
5+
## Overview
6+
Here you will probably give an overview of the API and add a description for it.
7+
8+
## Authentication
9+
Since the API requires authentication, you might want to add a section to describe the authentication flow as well.
10+
11+
## Errors
12+
Now this is the important section in this example. In this section, you can list the error responses that appear
13+
in every operation with some explanation. It can go sth like:
14+
15+
### 401 Unauthorized
16+
These errors are returned with the status code 401 whenever the authentication fails or a request is made to an
17+
endpoint without providing the authentication information as part of the request. Here are the 2 possible errors
18+
that can be returned.
19+
```json
20+
{
21+
"type": "client_error",
22+
"errors": [
23+
{
24+
"code": "authentication_failed",
25+
"detail": "Incorrect authentication credentials.",
26+
"attr": null
27+
}
28+
]
29+
}
30+
```
31+
```json
32+
{
33+
"type": "client_error",
34+
"errors": [
35+
{
36+
"code": "not_authenticated",
37+
"detail": "Authentication credentials were not provided.",
38+
"attr": null
39+
}
40+
]
41+
}
42+
```
43+
44+
### 405 Method Not Allowed
45+
This is returned when an endpoint is called with an unexpected http method. For example, if updating a user requires
46+
a POST request and a PATCH is issued instead, this error is returned. Here's how it looks like:
47+
48+
```json
49+
{
50+
"type": "client_error",
51+
"errors": [
52+
{
53+
"code": "method_not_allowed",
54+
"detail": "Method “patch” not allowed.",
55+
"attr": null
56+
}
57+
]
58+
}
59+
```
60+
61+
### 406 Not Acceptable
62+
This is returned if the `Accept` header is submitted and contains a value other than `application/json`. Here's how the response would look:
63+
64+
```json
65+
{
66+
"type": "client_error",
67+
"errors": [
68+
{
69+
"code": "not_acceptable",
70+
"detail": "Could not satisfy the request Accept header.",
71+
"attr": null
72+
}
73+
]
74+
}
75+
```
76+
77+
### 415 Unsupported Media Type
78+
This is returned when the request content type is not json. Here's how the response would look:
79+
80+
```json
81+
{
82+
"type": "client_error",
83+
"errors": [
84+
{
85+
"code": "not_acceptable",
86+
"detail": "Unsupported media type “application/xml” in request.",
87+
"attr": null
88+
}
89+
]
90+
}
91+
```
92+
93+
### 500 Internal Server Error
94+
This is returned when the API server encounters an unexpected error. Here's how the response would look:
95+
96+
```json
97+
{
98+
"type": "server_error",
99+
"errors": [
100+
{
101+
"code": "error",
102+
"detail": "A server error occurred.",
103+
"attr": null
104+
}
105+
]
106+
}
107+
```

docs/quickstart.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,8 @@ exception with
7878
instead of `return Response(data, status=500)`. That way, error response formatting is handled automatically for you.
7979
But, keep in mind that exceptions that result in 5xx response are reported to error monitoring tools (like Sentry)
8080
if you're using one.
81+
82+
## Integration with DRF spectacular
83+
If you plan to use [drf-spectacular](https://github.com/tfranzel/drf-spectacular) to generate an OpenAPI 3 schema,
84+
install with `pip install drf-standardized-errors[openapi]`. After that, check the doc page for configuring the
85+
integration.

docs/settings.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,46 @@ DRF_STANDARDIZED_ERRORS = {
1919
# {field}{NESTED_FIELD_SEPARATOR}{nested_field}
2020
# for example: 'shipping_address.zipcode'
2121
"NESTED_FIELD_SEPARATOR": ".",
22+
23+
# The below settings are for OpenAPI 3 schema generation
24+
25+
# ONLY the responses that correspond to these status codes will appear
26+
# in the API schema.
27+
"ALLOWED_ERROR_STATUS_CODES": [
28+
"400",
29+
"401",
30+
"403",
31+
"404",
32+
"405",
33+
"406",
34+
"415",
35+
"429",
36+
"500",
37+
],
38+
39+
# A mapping used to override the default serializers used to describe
40+
# the error response. The key is the status code and the value is a string
41+
# that represents the path to the serializer class that describes the
42+
# error response.
43+
"ERROR_SCHEMAS": None,
44+
45+
# When there is a validation error in list serializers, the "attr" returned
46+
# will be sth like "0.email", "1.email", "2.email", ... So, to describe
47+
# the error codes linked to the same field in a list serializer, the field
48+
# will appear in the schema with the name "INDEX.email"
49+
"LIST_INDEX_IN_API_SCHEMA": "INDEX",
50+
51+
# When there is a validation error in a DictField with the name "extra_data",
52+
# the "attr" returned will be sth like "extra_data.<key1>", "extra_data.<key2>",
53+
# "extra_data.<key3>", ... Since the keys of a DictField are not predetermined,
54+
# this setting is used as a common name to be used in the API schema. So, the
55+
# corresponding "attr" value for the previous example will be "extra_data.KEY"
56+
"DICT_KEY_IN_API_SCHEMA": "KEY",
57+
58+
# should be unique to error components since it is used to identify error
59+
# components generated dynamically to exclude them from being processed by
60+
# the postprocessing hook. This avoids raising warnings for "code" and "attr"
61+
# which can have the same choices across multiple serializers.
62+
"ERROR_COMPONENT_NAME_SUFFIX": "ErrorComponent",
2263
}
2364
```

0 commit comments

Comments
 (0)