github.com/whamcloud/lemur@v0.0.0-20190827193804-4655df8a52af/packaging/ci/lambda/GitPullS3/pygit2/errors.py (about)

     1  # -*- coding: utf-8 -*-
     2  #
     3  # Copyright 2010-2015 The pygit2 contributors
     4  #
     5  # This file is free software; you can redistribute it and/or modify
     6  # it under the terms of the GNU General Public License, version 2,
     7  # as published by the Free Software Foundation.
     8  #
     9  # In addition to the permissions in the GNU General Public License,
    10  # the authors give you unlimited permission to link the compiled
    11  # version of this file into combinations with other programs,
    12  # and to distribute those combinations without any restriction
    13  # coming from the use of this file.  (The General Public License
    14  # restrictions do apply in other respects; for example, they cover
    15  # modification of the file, and distribution when not linked into
    16  # a combined executable.)
    17  #
    18  # This file is distributed in the hope that it will be useful, but
    19  # WITHOUT ANY WARRANTY; without even the implied warranty of
    20  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    21  # General Public License for more details.
    22  #
    23  # You should have received a copy of the GNU General Public License
    24  # along with this program; see the file COPYING.  If not, write to
    25  # the Free Software Foundation, 51 Franklin Street, Fifth Floor,
    26  # Boston, MA 02110-1301, USA.
    27  
    28  # Import from pygit2
    29  from .ffi import ffi, C
    30  from _pygit2 import GitError
    31  
    32  
    33  value_errors = set([C.GIT_EEXISTS, C.GIT_EINVALIDSPEC, C.GIT_EEXISTS,
    34                      C.GIT_EAMBIGUOUS])
    35  
    36  def check_error(err, io=False):
    37      if err >= 0:
    38          return
    39  
    40      # Error message
    41      giterr = C.giterr_last()
    42      if giterr != ffi.NULL:
    43          message = ffi.string(giterr.message).decode()
    44      else:
    45          message = "err %d (no message provided)" % err
    46  
    47      # Translate to Python errors
    48      if err in value_errors:
    49          raise ValueError(message)
    50  
    51      if err == C.GIT_ENOTFOUND:
    52          if io:
    53              raise IOError(message)
    54  
    55          raise KeyError(message)
    56  
    57      if err == C.GIT_EINVALIDSPEC:
    58          raise ValueError(message)
    59  
    60      if err == C.GIT_ITEROVER:
    61          raise StopIteration()
    62  
    63      # Generic Git error
    64      raise GitError(message)
    65  
    66  # Indicate that we want libgit2 to pretend a function was not set
    67  Passthrough = Exception("The function asked for pass-through")