Skip to content

Commit dc9f972

Browse files
authored
Move ErrorResponse to its own file. (#1205)
This is part of the work for #1187.
1 parent edce377 commit dc9f972

3 files changed

Lines changed: 122 additions & 90 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ cmake-build-*/
1717
github-io-staging/
1818

1919
google/cloud/storage/testbench/__pycache__/
20-
google/cloud/storage/testbench/testbench.pyc
20+
google/cloud/storage/testbench/*.pyc
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
# Copyright 2018 Google LLC.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
"""A helper class to send error responses in the storage client test bench."""
16+
17+
import flask
18+
19+
20+
class ErrorResponse(Exception):
21+
"""Simplify generation of error responses."""
22+
status_code = 400
23+
24+
def __init__(self, message, status_code=None, payload=None):
25+
Exception.__init__(self)
26+
self.message = message
27+
if status_code is not None:
28+
self.status_code = status_code
29+
self.payload = payload
30+
31+
def as_response(self):
32+
kv = dict(self.payload or ())
33+
kv['message'] = self.message
34+
response = flask.jsonify(kv)
35+
response.status_code = self.status_code
36+
return response

0 commit comments

Comments
 (0)