Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add exception mappers for custom exceptions
* Exception mapper for TaskNotFound exceptions - returns a 404 Not Found response * Exception mapper for InvalidTaskData exceptions - returns a 400 Bad Request response
- Loading branch information
Showing
4 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package taskmanagement.api; | ||
|
||
public class ExceptionMessage { | ||
|
||
private final String message; | ||
|
||
public ExceptionMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/taskmanagement/api/InvalidTaskDataExceptionMapper.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package taskmanagement.api; | ||
|
||
import taskmanagement.exceptions.InvalidTaskDataException; | ||
|
||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
|
||
public class InvalidTaskDataExceptionMapper implements ExceptionMapper<InvalidTaskDataException> { | ||
|
||
@Override | ||
public Response toResponse(InvalidTaskDataException exception) { | ||
return Response | ||
.status(Response.Status.BAD_REQUEST) | ||
.entity(new ExceptionMessage(exception.getMessage())) | ||
.type(MediaType.APPLICATION_JSON) | ||
.build(); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/taskmanagement/api/TaskNotFoundExceptionMapper.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package taskmanagement.api; | ||
|
||
import taskmanagement.exceptions.TaskNotFoundException; | ||
|
||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
|
||
public class TaskNotFoundExceptionMapper implements ExceptionMapper<TaskNotFoundException> { | ||
|
||
@Override | ||
public Response toResponse(TaskNotFoundException exception) { | ||
return Response | ||
.status(Response.Status.NOT_FOUND) | ||
.entity(new ExceptionMessage(exception.getMessage())) | ||
.type(MediaType.APPLICATION_JSON) | ||
.build(); | ||
} | ||
|
||
} |