8000 Add global errorhandler (#88) · sesi/functions-framework-python@d3a200f · GitHub
[go: up one dir, main page]

Skip to content

Commit d3a200f

Browse files
authored
Add global errorhandler (GoogleCloudPlatform#88)
* Add global errorhandler * Add documentation
1 parent ba6a869 commit d3a200f

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,29 @@ curl localhost:8080
102102
# Output: Hello world!
103103
```
104104

105+
### Quickstart: Error handling
106+
107+
The framework includes an error handler that is similar to the
108+
[`flask.Flask.errorhandler`](https://flask.palletsprojects.com/en/1.1.x/api/#flask.Flask.errorhandler)
109+
function, which allows you to handle specific error types with a decorator:
110+
111+
```python
112+
import functions_framework
113+
114+
115+
@functions_framework.errorhandler(ZeroDivisionError)
116+
def handle_zero_division(e):
117+
return "I'm a teapot", 418
118+
119+
120+
def function(request):
121+
1 / 0
122+
return "Success", 200
123+
```
124+
125+
This function will catch the `ZeroDivisionError` and return a different
126+
response instead.
127+
105128
### Quickstart: Build a Deployable Container
106129

107130
1. Install [Docker](https://store.docker.com/search?type=edition&offering=community) and the [`pack` tool](https://buildpacks.io/docs/install-pack/).

src/functions_framework/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ def create_app(target=None, source=None, signature_type=None):
228228
# 5. Create the application
229229
app = flask.Flask(target, template_folder=template_folder)
230230
app.config["MAX_CONTENT_LENGTH"] = MAX_CONTENT_LENGTH
231+
global errorhandler
232+
errorhandler = app.errorhandler
231233

232234
# 6. Execute the module, within the application context
233235
with app.app_context():

tests/test_functions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,16 @@ def test_legacy_function_returns_none(monkeypatch):
359359

360360
assert resp.status_code == 200
361361
assert resp.data == b"OK"
362+
363+
364+
def test_errorhandler(monkeypatch):
365+
source = TEST_FUNCTIONS_DIR / "errorhandler" / "main.py"
366+
target = "function"
367+
368+
monkeypatch.setenv("ENTRY_POINT", target)
369+
370+
client = create_app(target, source).test_client()
371+
resp = client.get("/")
372+
373+
assert resp.status_code == 418
374+
assert resp.data == b"I'm a teapot"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import functions_framework
16+
17+
18+
@functions_framework.errorhandler(ZeroDivisionError)
19+
def handle_zero_division(e):
20+
return "I'm a teapot", 418
21+
22+
23+
def function(request):
24+
1 / 0
25+
return "Success", 200

0 commit comments

Comments
 (0)
0