~vishvananda/nova/network-refactor

« back to all changes in this revision

Viewing changes to nova/exception.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
# Copyright [2010] [Anso Labs, 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
 
 
16
"""
 
17
Nova base exception handling, including decorator for re-raising 
 
18
Nova-type exceptions. SHOULD include dedicated exception logging.
 
19
"""
 
20
 
 
21
import logging
 
22
import traceback
 
23
import sys
 
24
 
 
25
class Error(Exception):
 
26
    pass
 
27
 
 
28
class ApiError(Error): 
 
29
    def __init__(self, message='Unknown', code='Unknown'):
 
30
        self.message = message
 
31
        self.code = code
 
32
 
 
33
class NotFound(Error):
 
34
    pass
 
35
 
 
36
class NotAuthorized(Error):
 
37
    pass
 
38
 
 
39
def wrap_exception(f):
 
40
    def _wrap(*args, **kw):
 
41
        try:
 
42
            return f(*args, **kw)
 
43
        except Exception, e:
 
44
            if not isinstance(e, Error):
 
45
                # exc_type, exc_value, exc_traceback = sys.exc_info() 
 
46
                logging.exception('Uncaught exception')
 
47
                        # logging.debug(traceback.extract_stack(exc_traceback))
 
48
                raise Error(str(e))
 
49
            raise
 
50
    _wrap.func_name = f.func_name
 
51
    return _wrap
 
52
        
 
53