Package googleapiclient :: Module errors |
[hide private]
[frames] | no frames]

# Source Code for Module googleapiclient.errors

      1  # Copyright 2014 Google Inc. All Rights Reserved.
      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  """Errors for the library.
     16
     17  All exceptions defined by the library
     18  should be defined in this file.
     19  """
     20
     21  __author__ = 'jcgregorio@google.com (Joe Gregorio)'
     22
     23  import json
     24
     25  from oauth2client import util
     26
     27
     28 -class Error(Exception):
     29    """Base error for this module."""
     30    pass
     31
     32
     33 -class HttpError(Error):
     34    """HTTP data was invalid or unexpected."""
     35
     36    @util.positional(3)
     37 -  def __init__(self, resp, content, uri=None):
     38      self.resp = resp
     39      self.content = content
     40      self.uri = uri
     41
     42 -  def _get_reason(self):
     43      """Calculate the reason for the error from the response content."""
     44      reason = self.resp.reason
     45      try:
     46        data = json.loads(self.content)
     47        reason = data['error']['message']
     48      except (ValueError, KeyError):
     49        pass
     50      if reason is None:
     51        reason = ''
     52      return reason
     53
     54 -  def __repr__(self):
     55      if self.uri:
     56        return '<HttpError %s when requesting %s returned "%s">' % (
     57            self.resp.status, self.uri, self._get_reason().strip())
     58      else:
     59        return '<HttpError %s "%s">' % (self.resp.status, self._get_reason())
     60
     61    __str__ = __repr__
     62
     63
     64 -class InvalidJsonError(Error):
     65    """The JSON returned could not be parsed."""
     66    pass
     67
     68
     69 -class UnknownFileType(Error):
     70    """File type unknown or unexpected."""
     71    pass
     72
     73
     74 -class UnknownLinkType(Error):
     75    """Link type unknown or unexpected."""
     76    pass
     77
     78
     79 -class UnknownApiNameOrVersion(Error):
     80    """No API with that name and version exists."""
     81    pass
     82
     83
     84 -class UnacceptableMimeTypeError(Error):
     85    """That is an unacceptable mimetype for this operation."""
     86    pass
     87
     88
     89 -class MediaUploadSizeError(Error):
     90    """Media is larger than the method can accept."""
     91    pass
     92
     93
     94 -class ResumableUploadError(HttpError):
     95    """Error occured during resumable upload."""
     96    pass
     97
     98
     99 -class InvalidChunkSizeError(Error):
    100    """The given chunksize is not valid."""
    101    pass
    102
    103 -class InvalidNotificationError(Error):
    104    """The channel Notification is invalid."""
    105    pass
    106
    107 -class BatchError(HttpError):
    108    """Error occured during batch operations."""
    109
    110    @util.positional(2)
    111 -  def __init__(self, reason, resp=None, content=None):
    112      self.resp = resp
    113      self.content = content
    114      self.reason = reason
    115
    116 -  def __repr__(self):
    117        return '<BatchError %s "%s">' % (self.resp.status, self.reason)
    118
    119    __str__ = __repr__
    120
    121
    122 -class UnexpectedMethodError(Error):
    123    """Exception raised by RequestMockBuilder on unexpected calls."""
    124
    125    @util.positional(1)
    126 -  def __init__(self, methodId=None):
    127      """Constructor for an UnexpectedMethodError."""
    128      super(UnexpectedMethodError, self).__init__(
    129          'Received unexpected call %s' % methodId)
    130
    131
    132 -class UnexpectedBodyError(Error):
    133    """Exception raised by RequestMockBuilder on unexpected bodies."""
    134
    135 -  def __init__(self, expected, provided):
    136      """Constructor for an UnexpectedMethodError."""
    137      super(UnexpectedBodyError, self).__init__(
    138          'Expected: [%s] - Provided: [%s]' % (expected, provided))
    139