googleapiclient.errors

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