I am wondering what would be a cleaner way of dealing with this bunch of if clauses. As an aditional information, it's an exception handler to be used in a Spark Framework study project.
This piece of code does its job and it's readable, but a possible further extension would convert it into a monster. Do you have any suggestion to refactor it?
public ExceptionHandler<? super java.lang.Exception> handler() {
return (e, request, response) -> {
ErrorMessage error;
if (e instanceof SomeException1) {
response.status(422);
error = new ErrorMessage("message1", e.getMessage());
} else if (e instanceof SomeException2) {
response.status(404);
error = new ErrorMessage("message2", e.getMessage());
} else if (e instanceof SomeException3) {
response.status(500);
error = new ErrorMessage("message3", e.getMessage());
} else {
response.status(500);
error = new ErrorMessage("message4", e.getMessage());
}
[...]
};
}
Aucun commentaire:
Enregistrer un commentaire