blob: 0d420df1acccc925742ee5b564e0af7331101eaf [file] [log] [blame]
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -05001#!/usr/bin/python2.4
2#
Joe Gregorio20a5aa92011-04-01 17:44:25 -04003# Copyright (C) 2010 Google Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050016
17"""Errors for the library.
18
19All exceptions defined by the library
20should be defined in this file.
21"""
22
23__author__ = 'jcgregorio@google.com (Joe Gregorio)'
24
25
Ali Afshar2dcc6522010-12-16 10:11:53 +010026from anyjson import simplejson
27
28
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050029class Error(Exception):
30 """Base error for this module."""
31 pass
32
33
34class HttpError(Error):
35 """HTTP data was invalid or unexpected."""
36
Joe Gregorio49396552011-03-08 10:39:00 -050037 def __init__(self, resp, content, uri=None):
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050038 self.resp = resp
Ali Afshar2dcc6522010-12-16 10:11:53 +010039 self.content = content
Joe Gregorio49396552011-03-08 10:39:00 -050040 self.uri = uri
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050041
Ali Afshar2dcc6522010-12-16 10:11:53 +010042 def _get_reason(self):
Joe Gregoriofdf7c802011-06-30 12:33:38 -040043 """Calculate the reason for the error from the response content."""
Ali Afshar2dcc6522010-12-16 10:11:53 +010044 if self.resp.get('content-type', '').startswith('application/json'):
45 try:
46 data = simplejson.loads(self.content)
47 reason = data['error']['message']
48 except (ValueError, KeyError):
49 reason = self.content
50 else:
51 reason = self.resp.reason
52 return reason
53
54 def __repr__(self):
Joe Gregorio49396552011-03-08 10:39:00 -050055 if self.uri:
56 return '<HttpError %s when requesting %s returned "%s">' % (
57 self.resp.status, self.uri, self._get_reason())
58 else:
59 return '<HttpError %s "%s">' % (self.resp.status, self._get_reason())
Ali Afshar2dcc6522010-12-16 10:11:53 +010060
61 __str__ = __repr__
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050062
63
Joe Gregorio49396552011-03-08 10:39:00 -050064class InvalidJsonError(Error):
65 """The JSON returned could not be parsed."""
66 pass
67
68
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050069class UnknownLinkType(Error):
70 """Link type unknown or unexpected."""
71 pass
Joe Gregoriofdf7c802011-06-30 12:33:38 -040072
Joe Gregorio66f57522011-11-30 11:00:00 -050073
Joe Gregoriodae2f552011-11-21 08:16:56 -050074class UnknownApiNameOrVersion(Error):
Joe Gregorio8b4df3f2011-11-18 15:44:48 -050075 """No API with that name and version exists."""
76 pass
77
Joe Gregorioa388ce32011-09-09 17:19:13 -040078
Joe Gregoriofdf7c802011-06-30 12:33:38 -040079class UnacceptableMimeTypeError(Error):
80 """That is an unacceptable mimetype for this operation."""
81 pass
82
Joe Gregorioa388ce32011-09-09 17:19:13 -040083
Joe Gregoriofdf7c802011-06-30 12:33:38 -040084class MediaUploadSizeError(Error):
85 """Media is larger than the method can accept."""
86 pass
Joe Gregorioa388ce32011-09-09 17:19:13 -040087
88
Joe Gregoriod0bd3882011-11-22 09:49:47 -050089class ResumableUploadError(Error):
90 """Error occured during resumable upload."""
91 pass
92
93
Joe Gregorio66f57522011-11-30 11:00:00 -050094class BatchError(Error):
95 """Error occured during batch operations."""
96 pass
97
98
Joe Gregorioa388ce32011-09-09 17:19:13 -040099class UnexpectedMethodError(Error):
100 """Exception raised by RequestMockBuilder on unexpected calls."""
101
102 def __init__(self, methodId=None):
103 """Constructor for an UnexpectedMethodError."""
104 super(UnexpectedMethodError, self).__init__(
105 'Received unexpected call %s' % methodId)
106
107
108class UnexpectedBodyError(Error):
109 """Exception raised by RequestMockBuilder on unexpected bodies."""
110
111 def __init__(self, expected, provided):
112 """Constructor for an UnexpectedMethodError."""
113 super(UnexpectedBodyError, self).__init__(
114 'Expected: [%s] - Provided: [%s]' % (expected, provided))