Created
November 24, 2018 18:24
-
-
Save steklopod/21a41db71bfa0d55e3454df40830f1d8 to your computer and use it in GitHub Desktop.
ExceptionHandlers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.gazprombank.omnichannel.clientservice.configuration | |
import com.fasterxml.jackson.core.JsonGenerator | |
import com.fasterxml.jackson.databind.JsonSerializer | |
import com.fasterxml.jackson.databind.SerializerProvider | |
import com.fasterxml.jackson.databind.annotation.JsonSerialize | |
import org.springframework.http.HttpStatus | |
import org.springframework.web.bind.annotation.ControllerAdvice | |
import org.springframework.web.bind.annotation.ExceptionHandler | |
import org.springframework.web.bind.annotation.ResponseBody | |
import org.springframework.web.bind.annotation.ResponseStatus | |
import ru.gazprombank.omnichannel.clientservice.configuration.ErrorMessage.Error | |
import ru.gazprombank.omnichannel.clientservice.configuration.ErrorMessage.Message | |
import ru.gazprombank.webstarter.getLogger | |
import java.io.PrintWriter | |
import java.io.StringWriter | |
import java.lang.reflect.Field | |
import java.util.* | |
@ControllerAdvice | |
@ResponseStatus(HttpStatus.OK) | |
class ExceptionHandlers { | |
@ResponseBody | |
@ExceptionHandler(Exception::class) | |
fun rootExceptionHandler(e: Exception): ErrorMessage = when (e) { | |
is RuntimeException -> Error(e.stackTraceAsStringLn()) | |
else -> e.exceptionToWarningsArray() | |
} | |
companion object { | |
val log = getLogger() | |
} | |
} | |
@JsonSerialize(using = ErrorMessageSerializer::class) | |
sealed class ErrorMessage { | |
data class Message(val warnings: ArrayList<Warning>) : ErrorMessage() | |
data class Warning(val message: String, var systemId: String = "OMNI") : ErrorMessage() | |
data class Error(var message: String, var code: String = "exception") : ErrorMessage() | |
} | |
class ErrorMessageSerializer : JsonSerializer<ErrorMessage>() { | |
val log = getLogger() | |
override fun serialize(ex: ErrorMessage, jsonGenerator: JsonGenerator, sp: SerializerProvider) { | |
val typeOfException: String = ex.javaClass.toString().substringAfterLast("$").toLowerCase() | |
val fields: Array<Field> = ex.javaClass.declaredFields | |
// sp.config.with(SerializationFeature.WRAP_ROOT_VALUE) | |
with(jsonGenerator) { | |
writeStartObject() | |
writeStringField("status", typeOfException) | |
writeObjectFieldStart(typeOfException) | |
// TODO() | |
fields.forEach { | |
it.setAccessible(true) | |
writeObjectField(it.name, it.get(ex)) | |
} | |
writeEndObject() | |
writeEndObject() | |
} | |
} | |
} | |
private fun Exception.exceptionToWarningsArray(): Message { | |
//TODO | |
ExceptionHandlers.log.warn(this.printStackTrace().toString()) | |
val message = this.localizedMessage | |
val warnings = ErrorMessage.Warning(message) | |
val messages = arrayListOf(warnings) | |
return Message(messages) | |
} | |
fun Exception.stackTraceAsString2(): String { | |
val sw = StringWriter() | |
this.printStackTrace(PrintWriter(sw)) | |
return sw.toString() | |
} | |
fun Exception.stackTraceAsString() = Arrays.toString(this.getStackTrace()) | |
fun Exception.stackTraceAsStringLn() = this.stackTrace.map { | |
with(StringBuilder()) { | |
append(it.toString()) | |
append("\n") | |
} | |
}.toString() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment