github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/rest_api/sawtooth_rest_api/exceptions.py (about)

     1  # Copyright 2017 Intel Corporation
     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  
    16  import json
    17  from aiohttp.web import HTTPError
    18  
    19  
    20  class RestApiConfigurationError(Exception):
    21      pass
    22  
    23  
    24  class _ApiError(HTTPError):
    25      """A parent class for all REST API errors. Extends aiohttp's HTTPError,
    26      so instances will be caught automatically be the API, and turned into a
    27      response to send back to clients. Children should not define any methods,
    28      just four class variables which the parent __init__ will reference.
    29  
    30      Attributes:
    31          api_code (int): The fixed code to include in the JSON error response.
    32              Once established, this code should never change.
    33          status_code (int): HTTP status to use. Referenced withinin HTTPError's
    34              __init__ method.
    35          title (str): A short headline for the error.
    36          message (str): The human-readable description of the error.
    37  
    38      Raises:
    39          AssertionError: If api_code, status_code, title, or message were
    40              not set.
    41      """
    42      api_code = None
    43      status_code = None
    44      title = None
    45      message = None
    46  
    47      def __init__(self, additional_info=''):
    48          assert self.api_code is not None, 'Invalid ApiError, api_code not set'
    49          assert self.status_code is not None, 'Invalid ApiError, status not set'
    50          assert self.title is not None, 'Invalid ApiError, title not set'
    51          assert self.message is not None, 'Invalid ApiError, message not set'
    52  
    53          error = {
    54              'code': self.api_code,
    55              'title': self.title,
    56              'message': self.message + additional_info
    57          }
    58  
    59          super().__init__(
    60              content_type='application/json',
    61              text=json.dumps(
    62                  {'error': error},
    63                  indent=2,
    64                  separators=(',', ': '),
    65                  sort_keys=True))
    66  
    67  
    68  class UnknownValidatorError(_ApiError):
    69      api_code = 10
    70      status_code = 500
    71      title = 'Unknown Validator Error'
    72      message = ('An unknown error occurred with the validator while '
    73                 'processing your request.')
    74  
    75  
    76  class ValidatorNotReady(_ApiError):
    77      api_code = 15
    78      status_code = 503
    79      title = 'Validator Not Ready'
    80      message = ('The validator has no genesis block, and is not yet ready to '
    81                 'be queried. Try your request again later.')
    82  
    83  
    84  class ValidatorTimedOut(_ApiError):
    85      api_code = 17
    86      status_code = 503
    87      title = 'Validator Timed Out'
    88      message = ('The request timed out while waiting for a response from the '
    89                 'validator. Your request may or may not have been processed.')
    90  
    91  
    92  class ValidatorDisconnected(_ApiError):
    93      api_code = 18
    94      status_code = 503
    95      title = 'Validator Disconnected'
    96      message = ('The validator disconnected before sending a response. '
    97                 'Try your request again later.')
    98  
    99  
   100  class SendBackoffTimeout(_ApiError):
   101      api_code = 19
   102      status_code = 408
   103      title = 'Send timed out'
   104      message = ('Sending message to validator timed out. Retry limit reached. '
   105                 'Try your request again later.')
   106  
   107  
   108  class ValidatorResponseInvalid(_ApiError):
   109      api_code = 20
   110      status_code = 500
   111      title = 'Invalid Validator Response'
   112      message = ('The response from the validator could not be decoded. It may '
   113                 'have been corrupted or compromised.')
   114  
   115  
   116  class ResourceHeaderInvalid(_ApiError):
   117      api_code = 21
   118      status_code = 500
   119      title = 'Invalid Resource Header'
   120      message = ('The resource fetched from the validator had an invalid '
   121                 'header, and may be corrupted.')
   122  
   123  
   124  class StatusResponseMissing(_ApiError):
   125      api_code = 27
   126      status_code = 500
   127      title = 'Unable to Fetch Statuses'
   128      message = ('An unknown error occurred while attempting to fetch batch '
   129                 'statuses, and nothing was returned.')
   130  
   131  
   132  class SubmittedBatchesInvalid(_ApiError):
   133      api_code = 30
   134      status_code = 400
   135      title = 'Submitted Batches Invalid'
   136      message = ('The submitted BatchList was rejected by the validator. It was '
   137                 'poorly formed, or has an invalid signature.')
   138  
   139  
   140  class BatchQueueFull(_ApiError):
   141      api_code = 31
   142      status_code = 429
   143      title = 'Unable to Accept Batches'
   144      message = ('The validator cannot currently accept more batches, due to a '
   145                 'full queue.  Please submit your request again.')
   146  
   147  
   148  class NoBatchesSubmitted(_ApiError):
   149      api_code = 34
   150      status_code = 400
   151      title = 'No Batches Submitted'
   152      message = ('The protobuf BatchList you submitted was empty and contained '
   153                 'no Batches. You must submit at least one Batch.')
   154  
   155  
   156  class BadProtobufSubmitted(_ApiError):
   157      api_code = 35
   158      status_code = 400
   159      title = 'Protobuf Not Decodable'
   160      message = ('The protobuf BatchList you submitted was malformed and could '
   161                 'not be read.')
   162  
   163  
   164  class SubmissionWrongContentType(_ApiError):
   165      api_code = 42
   166      status_code = 400
   167      title = 'Wrong Content Type'
   168      message = ("Batches must be submitted in a BatchList protobuf binary, "
   169                 "with a 'Content-Type' header of 'application/octet-stream'.")
   170  
   171  
   172  class StatusWrongContentType(_ApiError):
   173      api_code = 43
   174      status_code = 400
   175      title = 'Wrong Content Type'
   176      message = ("Requests for batch statuses sent as a POST must have a "
   177                 "'Content-Type' header of 'application/json'.")
   178  
   179  
   180  class StatusBodyInvalid(_ApiError):
   181      api_code = 46
   182      status_code = 400
   183      title = 'Bad Status Request'
   184      message = ('Requests for batch statuses sent as a POST must have a JSON '
   185                 'formatted body with an array of at least one id string.')
   186  
   187  
   188  class HeadNotFound(_ApiError):
   189      api_code = 50
   190      status_code = 404
   191      title = 'Head Not Found'
   192      message = ("There is no block with the id specified in the 'head' "
   193                 "query parameter.")
   194  
   195  
   196  class CountInvalid(_ApiError):
   197      api_code = 53
   198      status_code = 400
   199      title = 'Invalid Count Query'
   200      message = ("The 'count' query parameter must be a positive, "
   201                 "non-zero integer.")
   202  
   203  
   204  class PagingInvalid(_ApiError):
   205      api_code = 54
   206      status_code = 400
   207      title = 'Invalid Paging Query'
   208      message = ("Paging request failed as written. One or more of the "
   209                 "'min', 'max', or 'count' query parameters were invalid or "
   210                 "out of range.")
   211  
   212  
   213  class SortInvalid(_ApiError):
   214      api_code = 57
   215      status_code = 400
   216      title = 'Invalid Sort Query'
   217      message = ("The sort request failed as written. Some of the keys "
   218                 "specified were not valid.")
   219  
   220  
   221  class InvalidResourceId(_ApiError):
   222      api_code = 60
   223      status_code = 400
   224      title = 'Invalid Resource Id'
   225      message = ('Blockchain items are identified by 128 character hex-strings. '
   226                 'A submitted block, batch, or transaction id was invalid: ')
   227  
   228  
   229  class InvalidStateAddress(_ApiError):
   230      api_code = 62
   231      status_code = 400
   232      title = 'Invalid State Address'
   233      message = ('The state address submitted was invalid. To fetch specific '
   234                 'state data, you must submit the full 70-character address.')
   235  
   236  
   237  class StatusIdQueryInvalid(_ApiError):
   238      api_code = 66
   239      status_code = 400
   240      title = 'Id Query Invalid or Missing'
   241      message = ("Requests for batch statuses sent as a GET request must have "
   242                 "an 'id' query parameter with a comma-separated list of "
   243                 "at least one batch id.")
   244  
   245  
   246  class BlockNotFound(_ApiError):
   247      api_code = 70
   248      status_code = 404
   249      title = 'Block Not Found'
   250      message = ('There is no block with the id specified in the blockchain.')
   251  
   252  
   253  class BatchNotFound(_ApiError):
   254      api_code = 71
   255      status_code = 404
   256      title = 'Batch Not Found'
   257      message = ('There is no batch with the id specified in the blockchain.')
   258  
   259  
   260  class TransactionNotFound(_ApiError):
   261      api_code = 72
   262      status_code = 404
   263      title = 'Transaction Not Found'
   264      message = ('There is no transaction with the id specified in the '
   265                 'blockchain.')
   266  
   267  
   268  class StateNotFound(_ApiError):
   269      api_code = 75
   270      status_code = 404
   271      title = 'State Not Found'
   272      message = ('There is no state data at the address specified.')
   273  
   274  
   275  class ReceiptNotFound(_ApiError):
   276      api_code = 80
   277      status_code = 404
   278      title = 'Transaction Receipt Not Found'
   279      message = ('There is no transaction receipt for the transaction id '
   280                 'specified in the receipt store.')
   281  
   282  
   283  class ReceiptWrongContentType(_ApiError):
   284      api_code = 81
   285      status_code = 400
   286      title = 'Wrong Content Type'
   287      message = ("Requests for transaction receipts sent as a POST must have a "
   288                 "'Content-Type' header of 'application/json'.")
   289  
   290  
   291  class ReceiptBodyInvalid(_ApiError):
   292      api_code = 82
   293      status_code = 400
   294      title = 'Bad Receipts Request'
   295      message = ('Requests for transaction receipts sent as a POST must have a '
   296                 'JSON formatted body with an array of at least one id string.')
   297  
   298  
   299  class ReceiptIdQueryInvalid(_ApiError):
   300      api_code = 83
   301      status_code = 400
   302      title = 'Id Query Invalid or Missing'
   303      message = ("Requests for transaction receipts sent as a GET request must "
   304                 "have an 'id' query parameter with a comma-separated list of "
   305                 "at least one transaction id.")