Example #1
0
def jb_exception_handler(exc, context):
    if isinstance(exc, exceptions.EntityNotFound):
        return ApiResponse.not_found(exc.error_code)
    if isinstance(exc, exceptions.BadDataException):
        return ApiResponse.bad_request(exc.error_code.dto_or_error_message, exc.error_code.error_code)
    if isinstance(exc, exceptions.JbAccessException):
        return ApiResponse.bad_request(exc.error_code.dto_or_error_message, exc.error_code.error_code)
    return exception_handler(exc, context)
Example #2
0
def error_handler(exc, context):
    if isinstance(exc, MethodNotAllowed):
        return ApiResponse.bad_request('Request method error')
    if isinstance(exc, ControlledError):
        if isinstance(exc, NotFoundError):
            return ApiResponse.not_found(exc.error_code)
        if isinstance(exc, NotUniqueError):
            return ApiResponse.bad_request(exc.error_code)
    return exception_handler(exc, context)
Example #3
0
 def post(self, request: HttpRequest):
     dto = CalculationRequestDto.from_dict(request.data)
     if not dto.is_valid():
         return ApiResponse.bad_request(dto)
     task = CalculationService.CalculationTask(arg1=dto.arg1,
                                               arg2=dto.arg2,
                                               operator=dto.operator)
     try:
         result = CalculationService.calculate(task)
         return ApiResponse.success(
             CalculationResultDto.from_calculation_result(result))
     except Exception as e:  # Should be more concrete
         return ApiResponse.bad_request(
             "Unable to calculate your expression: " + str(e))
Example #4
0
 def wrapped(*args, **kwargs):
     request = args[1]
     dto_object = dto_class.from_dict(request.data)
     if not dto_object.is_valid():
         return ApiResponse.bad_request(dto_object)
     kwargs.setdefault(dto_object_name, dto_object)
     return func(*args, **kwargs)
Example #5
0
    def post(self, request: HttpRequest):
        try:
            decoded_body = request.body.decode("utf-8")

            if request.content_type in self.URL_ENCODED_CONTENT_TYPES:
                decoded_body = urllib.parse.unquote_plus(
                    decoded_body)  #self._query_dict_to_str(request.POST)

            message = _helloService.echo(decoded_body)
            return ApiResponse.success(HelloResponseDto(message))

        except UnicodeDecodeError:
            return ApiResponse.bad_request(
                'Cannot convert POST data to string')
Example #6
0
 def post(self, request: HttpRequest, dto: ParentDto):
     if not dto.is_valid():
         return ApiResponse.bad_request(dto)
     return ApiResponse.success(dto)