This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherror_handler.py
155 lines (106 loc) · 4.42 KB
/
error_handler.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Copyright 2019 CNIT, Francesco Lombardo, Matteo Pergolesi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from flask import make_response, jsonify
from werkzeug.exceptions import HTTPException
def init_errorhandler(app):
def log_and_send(e):
data = {
"code": e.code,
"name": e.name,
"description": e.description,
}
app.logger.error('error: {}'.format(data))
return make_response(jsonify(data), data['code'])
# all 4xx and 500
@app.errorhandler(HTTPException)
def http_exception(e):
return log_and_send(e)
# all other unexpected exceptions
@app.errorhandler(Exception)
def any_exception(e):
# pass through HTTP errors
if isinstance(e, HTTPException):
return e
e.code = 500
e.name = 'unknown exception'
e.description = '{}: {}'.format(type(e).__name__, str(e))
return log_and_send(e)
class Error(Exception):
"""Base class for exceptions in this module."""
def __init__(self, description='Generic Error'):
self.description = description
def __str__(self):
return str(self.description)
class NfvoNotFound(Error):
def __init__(self, nfvo_id):
super().__init__(description='NFVO {0} not found.'.format(nfvo_id))
class RanoNotFound(Error):
def __init__(self, rano_id):
super().__init__(description='RANO {0} not found.'.format(rano_id))
class NfvoCredentialsNotFound(Error):
def __init__(self, nfvo_id):
super().__init__(
description='Credentials not found for NFVO {0}.'.format(nfvo_id))
class RanoCredentialsNotFound(Error):
def __init__(self, rano_id):
super().__init__(
description='Credentials not found for RANO {0}.'.format(rano_id))
class VimNotFound(Error):
def __init__(self):
super().__init__(description='Vim not found.')
class VimNetworkNotFound(Error):
def __init__(self, vim_network_name: str, site_name: str):
super().__init__(description=f'Network {vim_network_name} not found on site {site_name}.')
class NsOpNotFound(Error):
def __init__(self, ns_op_id=None):
super().__init__('NS LCM operation {0} not found.'.format(ns_op_id))
class NsNotFound(Error):
def __init__(self, ns_id=None):
super().__init__('NS instance {0} not found.'.format(ns_id))
class NsdNotFound(Error):
def __init__(self, nsd_id=None):
super().__init__('NS descriptor {0} not found.'.format(nsd_id))
class VnfNotFound(Error):
def __init__(self, vnf_id=None):
super().__init__('VNF instance {0} not found.'.format(vnf_id))
class VnfPkgNotFound(Error):
def __init__(self, vnfpkg_id=None):
super().__init__('VNF package {0} not found.'.format(vnfpkg_id))
class SubscriptionNotFound(Error):
def __init__(self, sub_id=None):
super().__init__('Subscription {0} not found.'.format(sub_id))
class BadRequest(Error):
def __init__(self, description='Bad request'):
super().__init__(description=description)
class Unauthorized(Error):
def __init__(self, description='Unauthorized'):
super().__init__(description=description)
class Forbidden(Error):
def __init__(self, description='Forbidden'):
super().__init__(description=description)
class ResourceNotFound(Error):
def __init__(self, description='Resource not found.'):
super().__init__(description=description)
class MethodNotAllowed(Error):
def __init__(self, description='Method Not Allowed'):
super().__init__(description=description)
class Conflict(Error):
def __init__(self, description='Conflict'):
super().__init__(description=description)
class Unprocessable(Error):
def __init__(self, description='Unprocessable Entity'):
super().__init__(description=description)
class ServerError(Error):
def __init__(self, description='Server error'):
super().__init__(description=description)