scots-codes 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- scots_codes-0.1.0/.github/workflows/publish.yml +28 -0
- scots_codes-0.1.0/.gitignore +9 -0
- scots_codes-0.1.0/.markdownlint.json +3 -0
- scots_codes-0.1.0/LICENSE +21 -0
- scots_codes-0.1.0/PKG-INFO +142 -0
- scots_codes-0.1.0/README.md +100 -0
- scots_codes-0.1.0/pyproject.toml +67 -0
- scots_codes-0.1.0/scots_codes/__init__.py +7 -0
- scots_codes-0.1.0/scots_codes/codes.py +126 -0
- scots_codes-0.1.0/scots_codes/fastapi.py +180 -0
- scots_codes-0.1.0/scots_codes/py.typed +0 -0
- scots_codes-0.1.0/tests/test_fastapi.py +244 -0
- scots_codes-0.1.0/uv.lock +957 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
publish:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
environment:
|
|
12
|
+
name: pypi
|
|
13
|
+
url: https://pypi.org/p/scots-codes
|
|
14
|
+
permissions:
|
|
15
|
+
id-token: write
|
|
16
|
+
contents: read
|
|
17
|
+
|
|
18
|
+
steps:
|
|
19
|
+
- uses: actions/checkout@v4
|
|
20
|
+
|
|
21
|
+
- name: Install uv
|
|
22
|
+
uses: astral-sh/setup-uv@v5
|
|
23
|
+
|
|
24
|
+
- name: Build distributions
|
|
25
|
+
run: uv build
|
|
26
|
+
|
|
27
|
+
- name: Publish to PyPI
|
|
28
|
+
run: uv publish
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 scots-codes contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: scots-codes
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Scottish HTTP status codes based on https://underwood.network/http/scots.html?via=dailydev
|
|
5
|
+
Project-URL: Reference, https://underwood.network/http/scots.html
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 scots-codes contributors
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Keywords: enum,fastapi,http,scots,scottish,status-codes
|
|
29
|
+
Classifier: Development Status :: 4 - Beta
|
|
30
|
+
Classifier: Intended Audience :: Developers
|
|
31
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
32
|
+
Classifier: Programming Language :: Python :: 3
|
|
33
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
34
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
35
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
36
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
37
|
+
Classifier: Typing :: Typed
|
|
38
|
+
Requires-Python: >=3.10
|
|
39
|
+
Provides-Extra: fastapi
|
|
40
|
+
Requires-Dist: fastapi>=0.100.0; extra == 'fastapi'
|
|
41
|
+
Description-Content-Type: text/markdown
|
|
42
|
+
|
|
43
|
+
# scots-codes
|
|
44
|
+
|
|
45
|
+
Little lib that provides reference to better status code descriptions.
|
|
46
|
+
|
|
47
|
+
Inspired by (litteraly copied from) [underwood.network/http/scots.html](https://underwood.network/http/scots.html)).
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
uv add scots-codes
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
With FastAPI integration:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
uv add "scots-codes[fastapi]"
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
from scots_codes import ScotsCode, get_scots_description
|
|
65
|
+
|
|
66
|
+
status = ScotsCode.THATS_IT_MADE
|
|
67
|
+
assert status == 201
|
|
68
|
+
assert status.value == 201
|
|
69
|
+
assert status.description == "THAT'S IT MADE"
|
|
70
|
+
assert status.phrase == "THAT'S IT MADE"
|
|
71
|
+
assert status.is_success
|
|
72
|
+
|
|
73
|
+
# Lookup by integer status code
|
|
74
|
+
ScotsCode(404) # ScotsCode.NO_FUCKIN_HERE_PAL
|
|
75
|
+
ScotsCode.from_code(418) # ScotsCode.AHM_A_FUCKIN_TEAPOT
|
|
76
|
+
ScotsCode.from_code(999) # None
|
|
77
|
+
get_scots_description(500) # "AH'VE FUCKED IT"
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## FastAPI Integration
|
|
81
|
+
|
|
82
|
+
Connect `scots-codes` to a FastAPI app with `setup_scots_errors` so default HTTP, validation, and server errors return the Scottish descriptions instead of standard phrases:
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from fastapi import FastAPI, HTTPException
|
|
86
|
+
from scots_codes import ScotsCode
|
|
87
|
+
from scots_codes.fastapi import ScotsHTTPException, setup_scots_errors
|
|
88
|
+
|
|
89
|
+
app = FastAPI()
|
|
90
|
+
setup_scots_errors(app)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@app.post("/items", status_code=ScotsCode.THATS_IT_MADE)
|
|
94
|
+
async def create_item() -> dict[str, str]:
|
|
95
|
+
return {"message": ScotsCode.THATS_IT_MADE.description}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@app.get("/secret")
|
|
99
|
+
async def secret() -> None:
|
|
100
|
+
raise HTTPException(status_code=403)
|
|
101
|
+
# -> 403 {"detail": "GET TAE FUCK"}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@app.get("/teapot")
|
|
105
|
+
async def teapot() -> None:
|
|
106
|
+
raise ScotsHTTPException(ScotsCode.AHM_A_FUCKIN_TEAPOT)
|
|
107
|
+
# -> 418 {"detail": "AH'M A FUCKIN TEAPOT"}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Configuration options
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
setup_scots_errors(
|
|
114
|
+
app,
|
|
115
|
+
override_custom_detail=False, # True replaces custom HTTPException(detail=...)
|
|
116
|
+
handle_validation_errors=True, # Handle RequestValidationError (422)
|
|
117
|
+
include_validation_errors=True, # Include Pydantic error list under "errors"
|
|
118
|
+
handle_server_errors=True, # Handle unhandled exceptions (500)
|
|
119
|
+
)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Development, packaging and publishing
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
# Install dependencies
|
|
126
|
+
uv sync
|
|
127
|
+
|
|
128
|
+
# Run tests, linting, and type checks
|
|
129
|
+
uv run pytest
|
|
130
|
+
uv run ruff check scots_codes tests
|
|
131
|
+
uv run mypy scots_codes tests
|
|
132
|
+
uv run pyright scots_codes tests
|
|
133
|
+
|
|
134
|
+
# Build sdist and wheel into dist/
|
|
135
|
+
uv build
|
|
136
|
+
|
|
137
|
+
# Publish to TestPyPI (token via UV_PUBLISH_TOKEN or --token)
|
|
138
|
+
uv publish --index testpypi
|
|
139
|
+
|
|
140
|
+
# Publish to PyPI (token via UV_PUBLISH_TOKEN or --token)
|
|
141
|
+
uv publish
|
|
142
|
+
```
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# scots-codes
|
|
2
|
+
|
|
3
|
+
Little lib that provides reference to better status code descriptions.
|
|
4
|
+
|
|
5
|
+
Inspired by (litteraly copied from) [underwood.network/http/scots.html](https://underwood.network/http/scots.html)).
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
uv add scots-codes
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
With FastAPI integration:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
uv add "scots-codes[fastapi]"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from scots_codes import ScotsCode, get_scots_description
|
|
23
|
+
|
|
24
|
+
status = ScotsCode.THATS_IT_MADE
|
|
25
|
+
assert status == 201
|
|
26
|
+
assert status.value == 201
|
|
27
|
+
assert status.description == "THAT'S IT MADE"
|
|
28
|
+
assert status.phrase == "THAT'S IT MADE"
|
|
29
|
+
assert status.is_success
|
|
30
|
+
|
|
31
|
+
# Lookup by integer status code
|
|
32
|
+
ScotsCode(404) # ScotsCode.NO_FUCKIN_HERE_PAL
|
|
33
|
+
ScotsCode.from_code(418) # ScotsCode.AHM_A_FUCKIN_TEAPOT
|
|
34
|
+
ScotsCode.from_code(999) # None
|
|
35
|
+
get_scots_description(500) # "AH'VE FUCKED IT"
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## FastAPI Integration
|
|
39
|
+
|
|
40
|
+
Connect `scots-codes` to a FastAPI app with `setup_scots_errors` so default HTTP, validation, and server errors return the Scottish descriptions instead of standard phrases:
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
from fastapi import FastAPI, HTTPException
|
|
44
|
+
from scots_codes import ScotsCode
|
|
45
|
+
from scots_codes.fastapi import ScotsHTTPException, setup_scots_errors
|
|
46
|
+
|
|
47
|
+
app = FastAPI()
|
|
48
|
+
setup_scots_errors(app)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@app.post("/items", status_code=ScotsCode.THATS_IT_MADE)
|
|
52
|
+
async def create_item() -> dict[str, str]:
|
|
53
|
+
return {"message": ScotsCode.THATS_IT_MADE.description}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@app.get("/secret")
|
|
57
|
+
async def secret() -> None:
|
|
58
|
+
raise HTTPException(status_code=403)
|
|
59
|
+
# -> 403 {"detail": "GET TAE FUCK"}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@app.get("/teapot")
|
|
63
|
+
async def teapot() -> None:
|
|
64
|
+
raise ScotsHTTPException(ScotsCode.AHM_A_FUCKIN_TEAPOT)
|
|
65
|
+
# -> 418 {"detail": "AH'M A FUCKIN TEAPOT"}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Configuration options
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
setup_scots_errors(
|
|
72
|
+
app,
|
|
73
|
+
override_custom_detail=False, # True replaces custom HTTPException(detail=...)
|
|
74
|
+
handle_validation_errors=True, # Handle RequestValidationError (422)
|
|
75
|
+
include_validation_errors=True, # Include Pydantic error list under "errors"
|
|
76
|
+
handle_server_errors=True, # Handle unhandled exceptions (500)
|
|
77
|
+
)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Development, packaging and publishing
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
# Install dependencies
|
|
84
|
+
uv sync
|
|
85
|
+
|
|
86
|
+
# Run tests, linting, and type checks
|
|
87
|
+
uv run pytest
|
|
88
|
+
uv run ruff check scots_codes tests
|
|
89
|
+
uv run mypy scots_codes tests
|
|
90
|
+
uv run pyright scots_codes tests
|
|
91
|
+
|
|
92
|
+
# Build sdist and wheel into dist/
|
|
93
|
+
uv build
|
|
94
|
+
|
|
95
|
+
# Publish to TestPyPI (token via UV_PUBLISH_TOKEN or --token)
|
|
96
|
+
uv publish --index testpypi
|
|
97
|
+
|
|
98
|
+
# Publish to PyPI (token via UV_PUBLISH_TOKEN or --token)
|
|
99
|
+
uv publish
|
|
100
|
+
```
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "scots-codes"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Scottish HTTP status codes based on https://underwood.network/http/scots.html?via=dailydev"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { file = "LICENSE" }
|
|
12
|
+
keywords = ["http", "status-codes", "scottish", "scots", "fastapi", "enum"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 4 - Beta",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.10",
|
|
19
|
+
"Programming Language :: Python :: 3.11",
|
|
20
|
+
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"Programming Language :: Python :: 3.13",
|
|
22
|
+
"Typing :: Typed",
|
|
23
|
+
]
|
|
24
|
+
dependencies = []
|
|
25
|
+
|
|
26
|
+
[project.optional-dependencies]
|
|
27
|
+
fastapi = ["fastapi>=0.100.0"]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Reference = "https://underwood.network/http/scots.html"
|
|
31
|
+
|
|
32
|
+
[dependency-groups]
|
|
33
|
+
dev = [
|
|
34
|
+
"fastapi>=0.115.0",
|
|
35
|
+
"httpx2>=0.1.0",
|
|
36
|
+
"mypy>=1.11.0",
|
|
37
|
+
"pyright>=1.1.380",
|
|
38
|
+
"pytest>=8.0.0",
|
|
39
|
+
"ruff>=0.7.0",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[[tool.uv.index]]
|
|
43
|
+
name = "testpypi"
|
|
44
|
+
url = "https://test.pypi.org/simple/"
|
|
45
|
+
publish-url = "https://test.pypi.org/legacy/"
|
|
46
|
+
explicit = true
|
|
47
|
+
|
|
48
|
+
[tool.hatch.build.targets.wheel]
|
|
49
|
+
packages = ["scots_codes"]
|
|
50
|
+
|
|
51
|
+
[tool.pytest.ini_options]
|
|
52
|
+
testpaths = ["tests"]
|
|
53
|
+
|
|
54
|
+
[tool.mypy]
|
|
55
|
+
strict = true
|
|
56
|
+
python_version = "3.10"
|
|
57
|
+
|
|
58
|
+
[tool.pyright]
|
|
59
|
+
pythonVersion = "3.10"
|
|
60
|
+
typeCheckingMode = "strict"
|
|
61
|
+
|
|
62
|
+
[tool.ruff]
|
|
63
|
+
target-version = "py310"
|
|
64
|
+
line-length = 100
|
|
65
|
+
|
|
66
|
+
[tool.ruff.lint]
|
|
67
|
+
select = ["E", "F", "I", "UP", "B", "SIM", "RUF"]
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from enum import IntEnum
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class ScotsCode(IntEnum):
|
|
7
|
+
description: str
|
|
8
|
+
|
|
9
|
+
def __new__(cls, value: int, description: str = "") -> ScotsCode:
|
|
10
|
+
obj = int.__new__(cls, value)
|
|
11
|
+
obj._value_ = value
|
|
12
|
+
obj.description = description
|
|
13
|
+
return obj
|
|
14
|
+
|
|
15
|
+
@property
|
|
16
|
+
def phrase(self) -> str:
|
|
17
|
+
"""Alias for ``description`` to match ``http.HTTPStatus.phrase``."""
|
|
18
|
+
return self.description
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def is_informational(self) -> bool:
|
|
22
|
+
return 100 <= self.value <= 199
|
|
23
|
+
|
|
24
|
+
@property
|
|
25
|
+
def is_success(self) -> bool:
|
|
26
|
+
return 200 <= self.value <= 299
|
|
27
|
+
|
|
28
|
+
@property
|
|
29
|
+
def is_redirection(self) -> bool:
|
|
30
|
+
return 300 <= self.value <= 399
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def is_client_error(self) -> bool:
|
|
34
|
+
return 400 <= self.value <= 499
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def is_server_error(self) -> bool:
|
|
38
|
+
return 500 <= self.value <= 599
|
|
39
|
+
|
|
40
|
+
@classmethod
|
|
41
|
+
def from_code(cls, code: int) -> ScotsCode | None:
|
|
42
|
+
try:
|
|
43
|
+
return cls(code)
|
|
44
|
+
except ValueError:
|
|
45
|
+
return None
|
|
46
|
+
|
|
47
|
+
# 1xx Informational
|
|
48
|
+
AYE_ON_YE_GO = (100, "AYE ON YE GO")
|
|
49
|
+
RIGHT_WERE_SWAPPIN = (101, "RIGHT WE'RE SWAPPIN")
|
|
50
|
+
AYE_HAUD_ON = (102, "AYE HAUD ON")
|
|
51
|
+
HERES_A_WEE_CLUE = (103, "HERE'S A WEE CLUE")
|
|
52
|
+
|
|
53
|
+
# 2xx Success
|
|
54
|
+
NAE_BOTHER = (200, "NAE BOTHER")
|
|
55
|
+
THATS_IT_MADE = (201, "THAT'S IT MADE")
|
|
56
|
+
AYE_AHLL_SEE_TAE_IT = (202, "AYE AH'LL SEE TAE IT")
|
|
57
|
+
DINNAE_QUOTE_ME = (203, "DINNAE QUOTE ME")
|
|
58
|
+
HEHAW = (204, "HEHAW")
|
|
59
|
+
CLEAR_THAT_AFF_AN_START_AGAIN = (205, "CLEAR THAT AFF AN START AGAIN")
|
|
60
|
+
HERES_A_WEE_BIT = (206, "HERE'S A WEE BIT")
|
|
61
|
+
BIT_O_THIS_BIT_O_THAT = (207, "BIT O THIS BIT O THAT")
|
|
62
|
+
AH_TELT_YE_AWREADY_YA_WEE_SCUNNER = (208, "AH TELT YE AWREADY YA WEE SCUNNER")
|
|
63
|
+
HERES_WHITS_CHANGED = (226, "HERE'S WHIT'S CHANGED")
|
|
64
|
+
|
|
65
|
+
# 3xx Redirection
|
|
66
|
+
TAKE_YER_PICK = (300, "TAKE YER PICK")
|
|
67
|
+
WEVE_FLITTED = (301, "WE'VE FLITTED")
|
|
68
|
+
WERE_OWER_THERE_THE_NOO = (302, "WE'RE OWER THERE THE NOO")
|
|
69
|
+
ASK_HIM_OWER_THERE = (303, "ASK HIM OWER THERE")
|
|
70
|
+
SAME_AULD_SHITE = (304, "SAME AULD SHITE")
|
|
71
|
+
GO_THROUGH_HIM = (305, "GO THROUGH HIM")
|
|
72
|
+
NAE_USE_TAE_ANYBODY = (306, "NAE USE TAE ANYBODY")
|
|
73
|
+
FUCK_OFF_OWER_THERE_FOR_NOO = (307, "FUCK OFF OWER THERE FOR NOO")
|
|
74
|
+
FUCK_OFF_OWER_THERE_FOR_GUID = (308, "FUCK OFF OWER THERE FOR GUID")
|
|
75
|
+
|
|
76
|
+
# 4xx Client Error
|
|
77
|
+
WHIT_THE_FUCKS_THIS = (400, "WHIT THE FUCK'S THIS")
|
|
78
|
+
WHO_THE_FUCK_ARE_YOU = (401, "WHO THE FUCK ARE YOU")
|
|
79
|
+
GIES_YER_MONEY = (402, "GIE'S YER MONEY")
|
|
80
|
+
GET_TAE_FUCK = (403, "GET TAE FUCK")
|
|
81
|
+
NO_FUCKIN_HERE_PAL = (404, "NO FUCKIN HERE PAL")
|
|
82
|
+
YE_CANNAE_DAE_THAT = (405, "YE CANNAE DAE THAT")
|
|
83
|
+
WEVE_NANE_O_THAT_KIND = (406, "WE'VE NANE O THAT KIND")
|
|
84
|
+
AN_WHOS_YER_PAL = (407, "AN WHO'S YER PAL")
|
|
85
|
+
AHVE_BEEN_STAUNIN_HERE_AGES = (408, "AH'VE BEEN STAUNIN HERE AGES")
|
|
86
|
+
PICK_A_FUCKIN_LANE = (409, "PICK A FUCKIN LANE")
|
|
87
|
+
DEID_AN_BURIED = (410, "DEID AN BURIED")
|
|
88
|
+
HOW_LANG_IS_IT_THEN = (411, "HOW LANG IS IT THEN")
|
|
89
|
+
THATS_NO_WHIT_WE_AGREED = (412, "THAT'S NO WHIT WE AGREED")
|
|
90
|
+
THATS_FUCKIN_MASSIVE = (413, "THAT'S FUCKIN MASSIVE")
|
|
91
|
+
AHM_NO_READIN_ALL_THAT = (414, "AH'M NO READIN ALL THAT")
|
|
92
|
+
NAE_IDEA_WHIT_THAT_IS = (415, "NAE IDEA WHIT THAT IS")
|
|
93
|
+
IT_DISNAE_GO_THAT_FAR = (416, "IT DISNAE GO THAT FAR")
|
|
94
|
+
AYE_YE_WISH = (417, "AYE YE WISH")
|
|
95
|
+
AHM_A_FUCKIN_TEAPOT = (418, "AH'M A FUCKIN TEAPOT")
|
|
96
|
+
WRONG_CUNT = (421, "WRONG CUNT")
|
|
97
|
+
AH_KEN_WHIT_YE_MEAN_BUT_NAW = (422, "AH KEN WHIT YE MEAN BUT NAW")
|
|
98
|
+
ITS_SNIBBED = (423, "IT'S SNIBBED")
|
|
99
|
+
THE_LAST_WAN_WENT_TITS_UP = (424, "THE LAST WAN WENT TITS UP")
|
|
100
|
+
HAUD_YER_HORSES = (425, "HAUD YER HORSES")
|
|
101
|
+
GET_YERSEL_A_NEW_WAN = (426, "GET YERSEL A NEW WAN")
|
|
102
|
+
CHECK_FIRST_YA_CHANCER = (428, "CHECK FIRST YA CHANCER")
|
|
103
|
+
SLOW_DOON = (429, "SLOW DOON")
|
|
104
|
+
TOO_MUCH_PISH = (431, "TOO MUCH PISH")
|
|
105
|
+
AWAY_YE_CLATTY_WEE_BASTARD = (444, "AWAY YE CLATTY WEE BASTARD")
|
|
106
|
+
THE_COUNCIL_SAYS_NAW = (451, "THE COUNCIL SAYS NAW")
|
|
107
|
+
|
|
108
|
+
# 5xx Server Error
|
|
109
|
+
AHVE_FUCKED_IT = (500, "AH'VE FUCKED IT")
|
|
110
|
+
AH_DINNAE_DAE_THAT = (501, "AH DINNAE DAE THAT")
|
|
111
|
+
THE_OTHER_BAMPOTS_BROKEN = (502, "THE OTHER BAMPOT'S BROKEN")
|
|
112
|
+
WERE_FUCKIN_SHUT = (503, "WE'RE FUCKIN SHUT")
|
|
113
|
+
THE_BASTARDS_NO_ANSWERIN = (504, "THE BASTARD'S NO ANSWERIN")
|
|
114
|
+
WHIT_FUCKIN_CENTURY_ARE_YE_IN = (505, "WHIT FUCKIN CENTURY ARE YE IN")
|
|
115
|
+
AHM_STAPPIT_FU = (507, "AH'M STAPPIT FU")
|
|
116
|
+
ROUND_AN_ROUND_YA_DAFT_TWAT = (508, "ROUND AN ROUND YA DAFT TWAT")
|
|
117
|
+
YELL_NEED_MAIR_THAN_THAT = (510, "YE'LL NEED MAIR THAN THAT")
|
|
118
|
+
LOG_IN_TAE_THE_WIFI_PAL = (511, "LOG IN TAE THE WIFI PAL")
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
StatusScode = ScotsCode
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def get_scots_description(code: int) -> str | None:
|
|
125
|
+
status = ScotsCode.from_code(code)
|
|
126
|
+
return status.description if status is not None else None
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import http.client
|
|
4
|
+
from collections.abc import Awaitable, Callable, Mapping
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from fastapi import FastAPI, HTTPException
|
|
8
|
+
from fastapi.encoders import jsonable_encoder
|
|
9
|
+
from fastapi.exceptions import RequestValidationError
|
|
10
|
+
from fastapi.utils import is_body_allowed_for_status_code
|
|
11
|
+
from starlette.exceptions import HTTPException as StarletteHTTPException
|
|
12
|
+
from starlette.requests import Request
|
|
13
|
+
from starlette.responses import JSONResponse, Response
|
|
14
|
+
|
|
15
|
+
from .codes import ScotsCode, get_scots_description
|
|
16
|
+
|
|
17
|
+
_FASTAPI_DEFAULT_DETAILS: dict[int, frozenset[str]] = {
|
|
18
|
+
401: frozenset({"Not authenticated"}),
|
|
19
|
+
403: frozenset({"Not authenticated"}),
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
HTTPExceptionHandler = Callable[[Request, Exception], Awaitable[Response]]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class ScotsHTTPException(HTTPException):
|
|
26
|
+
"""``fastapi.HTTPException`` that defaults ``detail`` to the Scottish description."""
|
|
27
|
+
|
|
28
|
+
def __init__(
|
|
29
|
+
self,
|
|
30
|
+
status_code: int,
|
|
31
|
+
detail: Any = None,
|
|
32
|
+
headers: Mapping[str, str] | None = None,
|
|
33
|
+
) -> None:
|
|
34
|
+
if detail is None:
|
|
35
|
+
detail = get_scots_description(status_code)
|
|
36
|
+
super().__init__(status_code=status_code, detail=detail, headers=headers)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _is_default_detail(status_code: int, detail: Any) -> bool:
|
|
40
|
+
if detail is None or detail == "":
|
|
41
|
+
return True
|
|
42
|
+
if not isinstance(detail, str):
|
|
43
|
+
return False
|
|
44
|
+
if detail == http.client.responses.get(status_code, ""):
|
|
45
|
+
return True
|
|
46
|
+
return detail in _FASTAPI_DEFAULT_DETAILS.get(status_code, frozenset())
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def make_scots_http_exception_handler(
|
|
50
|
+
*,
|
|
51
|
+
override_custom_detail: bool = False,
|
|
52
|
+
) -> HTTPExceptionHandler:
|
|
53
|
+
"""Create a FastAPI/Starlette exception handler for ``HTTPException``."""
|
|
54
|
+
|
|
55
|
+
async def handler(request: Request, exc: Exception) -> Response:
|
|
56
|
+
if not isinstance(exc, StarletteHTTPException):
|
|
57
|
+
raise exc
|
|
58
|
+
|
|
59
|
+
headers = exc.headers
|
|
60
|
+
if not is_body_allowed_for_status_code(exc.status_code):
|
|
61
|
+
return Response(status_code=exc.status_code, headers=headers)
|
|
62
|
+
|
|
63
|
+
detail: Any = exc.detail
|
|
64
|
+
scots_description = get_scots_description(exc.status_code)
|
|
65
|
+
if scots_description is not None and (
|
|
66
|
+
override_custom_detail or _is_default_detail(exc.status_code, detail)
|
|
67
|
+
):
|
|
68
|
+
detail = scots_description
|
|
69
|
+
|
|
70
|
+
return JSONResponse(
|
|
71
|
+
content={"detail": detail},
|
|
72
|
+
status_code=exc.status_code,
|
|
73
|
+
headers=headers,
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
return handler
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def make_scots_validation_exception_handler(
|
|
80
|
+
*,
|
|
81
|
+
include_validation_errors: bool = True,
|
|
82
|
+
) -> HTTPExceptionHandler:
|
|
83
|
+
"""Create a FastAPI exception handler for ``RequestValidationError``."""
|
|
84
|
+
|
|
85
|
+
async def handler(request: Request, exc: Exception) -> Response:
|
|
86
|
+
if not isinstance(exc, RequestValidationError):
|
|
87
|
+
raise exc
|
|
88
|
+
|
|
89
|
+
content: dict[str, Any] = {
|
|
90
|
+
"detail": ScotsCode.AH_KEN_WHIT_YE_MEAN_BUT_NAW.description,
|
|
91
|
+
}
|
|
92
|
+
if include_validation_errors:
|
|
93
|
+
content["errors"] = jsonable_encoder(exc.errors())
|
|
94
|
+
|
|
95
|
+
return JSONResponse(
|
|
96
|
+
status_code=ScotsCode.AH_KEN_WHIT_YE_MEAN_BUT_NAW,
|
|
97
|
+
content=content,
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
return handler
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
async def scots_http_exception_handler(request: Request, exc: Exception) -> Response:
|
|
104
|
+
"""Default Scottish handler for ``StarletteHTTPException`` / ``fastapi.HTTPException``."""
|
|
105
|
+
return await _DEFAULT_HTTP_EXCEPTION_HANDLER(request, exc)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
async def scots_validation_exception_handler(request: Request, exc: Exception) -> Response:
|
|
109
|
+
"""Default Scottish handler for ``RequestValidationError``."""
|
|
110
|
+
return await _DEFAULT_VALIDATION_EXCEPTION_HANDLER(request, exc)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
async def scots_server_error_handler(request: Request, exc: Exception) -> Response:
|
|
114
|
+
"""Scottish handler for unhandled 500 Internal Server Errors."""
|
|
115
|
+
return JSONResponse(
|
|
116
|
+
status_code=ScotsCode.AHVE_FUCKED_IT,
|
|
117
|
+
content={"detail": ScotsCode.AHVE_FUCKED_IT.description},
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
_DEFAULT_HTTP_EXCEPTION_HANDLER = make_scots_http_exception_handler()
|
|
122
|
+
_DEFAULT_VALIDATION_EXCEPTION_HANDLER = make_scots_validation_exception_handler()
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def setup_scots_errors(
|
|
126
|
+
app: FastAPI,
|
|
127
|
+
*,
|
|
128
|
+
override_custom_detail: bool = False,
|
|
129
|
+
handle_validation_errors: bool = True,
|
|
130
|
+
include_validation_errors: bool = True,
|
|
131
|
+
handle_server_errors: bool = True,
|
|
132
|
+
) -> FastAPI:
|
|
133
|
+
"""Register Scottish HTTP error handlers on a FastAPI application.
|
|
134
|
+
|
|
135
|
+
Args:
|
|
136
|
+
app: The ``FastAPI`` application instance.
|
|
137
|
+
override_custom_detail: When ``True``, replaces even custom ``HTTPException.detail``
|
|
138
|
+
values with the matching Scottish description. When ``False`` (default),
|
|
139
|
+
only default HTTP/FastAPI error messages are replaced and custom details
|
|
140
|
+
are preserved.
|
|
141
|
+
handle_validation_errors: When ``True`` (default), handles
|
|
142
|
+
``RequestValidationError`` (422) with Scottish error detail.
|
|
143
|
+
include_validation_errors: When ``True`` (default), keeps the encoded Pydantic
|
|
144
|
+
validation errors list under the ``"errors"`` key alongside ``"detail"``.
|
|
145
|
+
handle_server_errors: When ``True`` (default), returns JSON with the Scottish
|
|
146
|
+
500 description on unhandled server exceptions.
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
The same ``FastAPI`` instance for optional chaining.
|
|
150
|
+
"""
|
|
151
|
+
app.add_exception_handler(
|
|
152
|
+
StarletteHTTPException,
|
|
153
|
+
make_scots_http_exception_handler(
|
|
154
|
+
override_custom_detail=override_custom_detail,
|
|
155
|
+
),
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
if handle_validation_errors:
|
|
159
|
+
app.add_exception_handler(
|
|
160
|
+
RequestValidationError,
|
|
161
|
+
make_scots_validation_exception_handler(
|
|
162
|
+
include_validation_errors=include_validation_errors,
|
|
163
|
+
),
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
if handle_server_errors:
|
|
167
|
+
app.add_exception_handler(Exception, scots_server_error_handler)
|
|
168
|
+
|
|
169
|
+
return app
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
__all__ = [
|
|
173
|
+
"ScotsHTTPException",
|
|
174
|
+
"make_scots_http_exception_handler",
|
|
175
|
+
"make_scots_validation_exception_handler",
|
|
176
|
+
"scots_http_exception_handler",
|
|
177
|
+
"scots_server_error_handler",
|
|
178
|
+
"scots_validation_exception_handler",
|
|
179
|
+
"setup_scots_errors",
|
|
180
|
+
]
|
|
File without changes
|