def _create_data_error_response(self, message, obj):
     return ErrorResponseSchema().dump(
         types.ErrorResponse(
             status_code=400,
             message=message,
             errors=[
                 types.ConcurrentModificationError(
                     message=message, current_version=obj["version"])
             ],
         ))
 def _create_version_error_response(self, version):
     return ErrorResponseSchema().dump(
         types.ErrorResponse(
             status_code=409,
             message="Version mismatch. Concurrent modification.",
             errors=[
                 types.ConcurrentModificationError(
                     message="Version mismatch. Concurrent modification.",
                     current_version=version,
                 )
             ],
         ))
    def _process_error(self, response: requests.Response) -> None:
        correlation_id = response.headers.get(HEADER_CORRELATION_ID)
        if not response.content:
            response.raise_for_status()
        obj = ErrorResponseSchema().loads(response.content)

        # We'll fetch the 'raw' errors from the response because some of the
        # attributes are not included in the schemas.
        # With the raw errors in the CommercetoolsError object we can use that
        # information later to render more detailed error messages
        errors_raw = []
        try:
            response_json = response.json()
        except ValueError:
            pass
        else:
            errors_raw = response_json.get("errors", [])

        raise CommercetoolsError(obj.message, errors_raw, obj, correlation_id)
 def get_by_container_key(self, request, container: str, key: str):
     id = (container, key)
     item = self.model.objects.get(id)
     if item:
         return create_commercetools_response(request, json=item)
     else:
         content = ErrorResponseSchema().dumps(
             types.ErrorResponse(
                 status_code=404,
                 message=f"The CustomObject with ID '({container},{key})'",
                 errors=[
                     types.InvalidSubjectError(
                         code="InvalidSubject",
                         message=
                         f"The CustomObject with ID '({container},{key}' was not found.",  # noqa E501
                     )
                 ],
             ))
         return create_commercetools_response(request,
                                              text=content,
                                              status_code=404)
Exemplo n.º 5
0
    def create(self, request):
        obj = self._schema_draft().loads(request.body)

        if isinstance(obj.destination, types.SqsDestination):
            dest = obj.destination
            message = (
                "A test message could not be delivered to this destination: "
                "SQS %r in %r for %r. "
                "Please make sure your destination is correctly configured."
            ) % (dest.queue_url, dest.region, dest.access_key)
            error = types.ErrorResponse(
                status_code=400,
                message=message,
                errors=[types.InvalidInputError(message=message)],
            )
            error_data = ErrorResponseSchema().dumps(error).encode("utf-8")
            return create_commercetools_response(request,
                                                 content=error_data,
                                                 status_code=400)

        data = self.model.add(obj)
        return create_commercetools_response(request, json=data)