/ justapi / tests / test_exceptions.py
swadhinbiswas test: add end-to-end integration test suite with gRPC and proto stubs · 7/9/2026
43e015f
32 lines · 909 Bytes
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
import threading
import time
import urllib.request
import urllib.error
from justapi import JustAPIApp, Path, RequestValidationError, JSONResponse

app = JustAPIApp()

@app.get("/items/{item_id}")
def get_item(item_id: int = Path()):
    return {"item": item_id}

def validation_exception_handler(request, exc):
    return JSONResponse({"detail": exc.errors}, status_code=422)

app.add_exception_handler(RequestValidationError, validation_exception_handler)

def run_server():
    app.run("127.0.0.1:8084")

if __name__ == "__main__":
    t = threading.Thread(target=run_server, daemon=True)
    t.start()
    time.sleep(1)

    try:
        req = urllib.request.Request("http://127.0.0.1:8084/items/abc")
        with urllib.request.urlopen(req) as response:
            print("Success:", response.read().decode())
    except urllib.error.HTTPError as e:
        print(f"Error {e.code}: {e.read().decode()}")