summaryrefslogtreecommitdiff
path: root/topics/gn3-error-handling.gmi
blob: b6aed81e3c8cbee345516bfc8a514b9f0191bde9 (plain)
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
# GN3 Error Handling

## Tags

* type: docs, documentation
* status: open, ongoing
* keywords: gn3, error_handling, exceptions, docs, documentation

## Introduction
This documentation explains how GN3 handles errors in the application.

## Notes
GN3 uses app-level error handling in Flask
instead of try and except blocks within individual routes.

## Benefits

* Centralized error management simplifies maintenance by centralizing error handling in one location.
* Ensures uniform behavior across the application, making it more predictable.
* Promotes code reuse by handling errors through shared logic.
* Keeps route functions clean and focused by removing redundant error handling.
* Enhances scalability by efficiently managing error handling as the application grows.

In this approach, errors are registered and handled at the application level. For example:

## Error Handler

```python
def handle_generic(exc: Exception) -> Response:
    """Handle generic exceptions."""
    current_app.logger.error(exc)
    response = jsonify({
        "error": type(exc).__name__,
        "error_description": exc.args[0] if exc.args else "Generic Exception",
        "trace": traceback.format_exc()
    })
    response.status_code = 500
    return response
```

## Registering Error Handlers at the App Level

```python
def register_error_handlers(app: Flask):
    """Register application-level error handlers."""
    app.register_error_handler(NotFound, page_not_found)
    app.register_error_handler(Exception, handle_generic)
```

## Flask Route Without Try-Except Blocks

```python
@app.route('/some_route')
def some_route():
    # If an error occurs, it is handled at the app level
    data = computation()
    return jsonify(data)
```

For more examples in GN3, see the link below:

=> https://github.com/genenetwork/genenetwork3/blob/main/gn3/errors.py

Related issues:
=> https://issues.genenetwork.org/issues/error-handling-external-errors