Skip to content

Commit

Permalink
fix: Client exception deserializers added
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz.gryzbon committed Apr 19, 2022
1 parent eef93b9 commit a3ff96c
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package com.lsdconsulting.exceptionhandling.client.exception

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.lsdconsulting.exceptionhandling.api.ErrorResponse
import org.springframework.http.HttpStatus.BAD_GATEWAY

@JsonDeserialize(using = BadGatewayExceptionDeserializer::class)
class BadGatewayException : ErrorResponseException {
constructor(errorResponse: ErrorResponse?) : super(errorResponse, BAD_GATEWAY)
constructor(message: String) : super(message, BAD_GATEWAY)
}

class BadGatewayExceptionDeserializer : JsonDeserializer<BadGatewayException>() {
override fun deserialize(jp: JsonParser, dc: DeserializationContext): BadGatewayException {
val errorResponse = jp.readValueAs(ErrorResponse::class.java)
return BadGatewayException(errorResponse)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package com.lsdconsulting.exceptionhandling.client.exception

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.lsdconsulting.exceptionhandling.api.ErrorResponse
import org.springframework.http.HttpStatus.BAD_REQUEST

@JsonDeserialize(using = BadRequestExceptionDeserializer::class)
class BadRequestException : ErrorResponseException {
constructor(errorResponse: ErrorResponse?) : super(errorResponse, BAD_REQUEST)
constructor(message: String) : super(message, BAD_REQUEST)
}

class BadRequestExceptionDeserializer : JsonDeserializer<BadRequestException>() {
override fun deserialize(jp: JsonParser, dc: DeserializationContext): BadRequestException {
val errorResponse = jp.readValueAs(ErrorResponse::class.java)
return BadRequestException(errorResponse)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package com.lsdconsulting.exceptionhandling.client.exception

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.lsdconsulting.exceptionhandling.api.ErrorResponse
import org.springframework.http.HttpStatus.CONFLICT

@JsonDeserialize(using = ConflictExceptionDeserializer::class)
class ConflictException : ErrorResponseException {
constructor(errorResponse: ErrorResponse?) : super(errorResponse, CONFLICT)
constructor(message: String) : super(message, CONFLICT)
}

class ConflictExceptionDeserializer : JsonDeserializer<ConflictException>() {
override fun deserialize(jp: JsonParser, dc: DeserializationContext): ConflictException {
val errorResponse = jp.readValueAs(ErrorResponse::class.java)
return ConflictException(errorResponse)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package com.lsdconsulting.exceptionhandling.client.exception

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.lsdconsulting.exceptionhandling.api.ErrorResponse
import org.springframework.http.HttpStatus.GATEWAY_TIMEOUT

@JsonDeserialize(using = GatewayTimeoutExceptionDeserializer::class)
class GatewayTimeoutException : ErrorResponseException {
constructor(errorResponse: ErrorResponse?) : super(errorResponse, GATEWAY_TIMEOUT)
constructor(message: String) : super(message, GATEWAY_TIMEOUT)
}

class GatewayTimeoutExceptionDeserializer : JsonDeserializer<GatewayTimeoutException>() {
override fun deserialize(jp: JsonParser, dc: DeserializationContext): GatewayTimeoutException {
val errorResponse = jp.readValueAs(ErrorResponse::class.java)
return GatewayTimeoutException(errorResponse)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package com.lsdconsulting.exceptionhandling.client.exception

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.lsdconsulting.exceptionhandling.api.ErrorResponse
import org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR

@JsonDeserialize(using = InternalServerExceptionDeserializer::class)
class InternalServerException : ErrorResponseException {
constructor(errorResponse: ErrorResponse?) : super(errorResponse, INTERNAL_SERVER_ERROR)
constructor(message: String) : super(message, INTERNAL_SERVER_ERROR)
}

class InternalServerExceptionDeserializer : JsonDeserializer<InternalServerException>() {
override fun deserialize(jp: JsonParser, dc: DeserializationContext): InternalServerException {
val errorResponse = jp.readValueAs(ErrorResponse::class.java)
return InternalServerException(errorResponse)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package com.lsdconsulting.exceptionhandling.client.exception

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.lsdconsulting.exceptionhandling.api.ErrorResponse
import org.springframework.http.HttpStatus.NOT_FOUND

@JsonDeserialize(using = NotFoundExceptionDeserializer::class)
class NotFoundException : ErrorResponseException {
constructor(errorResponse: ErrorResponse?) : super(errorResponse, NOT_FOUND)
constructor(message: String) : super(message, NOT_FOUND)
}

class NotFoundExceptionDeserializer : JsonDeserializer<NotFoundException>() {
override fun deserialize(jp: JsonParser, dc: DeserializationContext): NotFoundException {
val errorResponse = jp.readValueAs(ErrorResponse::class.java)
return NotFoundException(errorResponse)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package com.lsdconsulting.exceptionhandling.client.exception

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.lsdconsulting.exceptionhandling.api.ErrorResponse
import org.springframework.http.HttpStatus.NOT_IMPLEMENTED

@JsonDeserialize(using = NotImplementedExceptionDeserializer::class)
class NotImplementedException : ErrorResponseException {
constructor(errorResponse: ErrorResponse?) : super(errorResponse, NOT_IMPLEMENTED)
constructor(message: String) : super(message, NOT_IMPLEMENTED)
}

class NotImplementedExceptionDeserializer : JsonDeserializer<NotImplementedException>() {
override fun deserialize(jp: JsonParser, dc: DeserializationContext): NotImplementedException {
val errorResponse = jp.readValueAs(ErrorResponse::class.java)
return NotImplementedException(errorResponse)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package com.lsdconsulting.exceptionhandling.client.exception

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.lsdconsulting.exceptionhandling.api.ErrorResponse
import org.springframework.http.HttpStatus.PRECONDITION_FAILED

@JsonDeserialize(using = PreconditionFailedExceptionDeserializer::class)
class PreconditionFailedException : ErrorResponseException {
constructor(errorResponse: ErrorResponse?) : super(errorResponse, PRECONDITION_FAILED)
constructor(message: String) : super(message, PRECONDITION_FAILED)
}

class PreconditionFailedExceptionDeserializer : JsonDeserializer<PreconditionFailedException>() {
override fun deserialize(jp: JsonParser, dc: DeserializationContext): PreconditionFailedException {
val errorResponse = jp.readValueAs(ErrorResponse::class.java)
return PreconditionFailedException(errorResponse)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package com.lsdconsulting.exceptionhandling.client.exception

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.lsdconsulting.exceptionhandling.api.ErrorResponse
import org.springframework.http.HttpStatus.SERVICE_UNAVAILABLE

@JsonDeserialize(using = ServiceUnavailableExceptionDeserializer::class)
class ServiceUnavailableException : ErrorResponseException {
constructor(errorResponse: ErrorResponse?) : super(errorResponse, SERVICE_UNAVAILABLE)
constructor(message: String) : super(message, SERVICE_UNAVAILABLE)
}

class ServiceUnavailableExceptionDeserializer : JsonDeserializer<ServiceUnavailableException>() {
override fun deserialize(jp: JsonParser, dc: DeserializationContext): ServiceUnavailableException {
val errorResponse = jp.readValueAs(ErrorResponse::class.java)
return ServiceUnavailableException(errorResponse)
}
}

0 comments on commit a3ff96c

Please sign in to comment.