Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ResponseStatusException added #82

Merged
merged 3 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 5 additions & 32 deletions src/main/java/git/tracehub/pmo/controller/AdviceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
package git.tracehub.pmo.controller;

import git.tracehub.pmo.exception.Logged;
import git.tracehub.pmo.exception.ResourceAlreadyExistsException;
import git.tracehub.pmo.exception.ResourceNotFoundException;
import git.tracehub.pmo.exception.RestError;
import java.util.stream.Collectors;
import org.springframework.context.support.DefaultMessageSourceResolvable;
Expand All @@ -35,8 +33,8 @@
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.server.ResponseStatusException;

/**
* Exception handler.
Expand All @@ -54,7 +52,6 @@ public class AdviceController {
* @return ResponseEntity
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<byte[]> handle(
final MethodArgumentNotValidException exception
) {
Expand All @@ -71,21 +68,20 @@ public ResponseEntity<byte[]> handle(
}

/**
* Handle ResourceAlreadyExistsException.
* Handle ResponseStatusException.
*
* @param exception Exception
* @return ResponseEntity
*/
@ExceptionHandler(ResourceAlreadyExistsException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ResponseStatusException.class)
public ResponseEntity<byte[]> handle(
final ResourceAlreadyExistsException exception
final ResponseStatusException exception
) {
return new Logged(
exception,
new RestError(
exception.getMessage(),
HttpStatus.BAD_REQUEST
HttpStatus.valueOf(exception.getStatusCode().value())
)
).value();
}
Expand All @@ -97,7 +93,6 @@ public ResponseEntity<byte[]> handle(
* @return ResponseEntity
*/
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<byte[]> handle(
final IllegalArgumentException exception
) {
Expand All @@ -110,34 +105,13 @@ public ResponseEntity<byte[]> handle(
).value();
}

/**
* Handle ResourceNotFoundException.
*
* @param exception Exception
* @return ResponseEntity
*/
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<byte[]> handle(
final ResourceNotFoundException exception
) {
return new Logged(
exception,
new RestError(
exception.getMessage(),
HttpStatus.NOT_FOUND
)
).value();
}

/**
* Handle AccessDeniedException.
*
* @param exception Exception
* @return ResponseEntity
*/
@ExceptionHandler(AccessDeniedException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
public ResponseEntity<byte[]> handle(
final AccessDeniedException exception
) {
Expand All @@ -157,7 +131,6 @@ public ResponseEntity<byte[]> handle(
* @return ResponseEntity
*/
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseEntity<byte[]> handle(final Exception exception) {
return new Logged(
exception,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,22 @@

package git.tracehub.pmo.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

/**
* ResourceAlreadyExistsException.
*
* @since 0.0.0
*/
public class ResourceAlreadyExistsException extends RuntimeException {
public class ResourceAlreadyExistsException extends ResponseStatusException {

/**
* Constructor.
*
* @param message Message
*/
public ResourceAlreadyExistsException(final String message) {
super(message);
super(HttpStatus.CONFLICT, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,22 @@

package git.tracehub.pmo.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

/**
* ResourceNotFoundException.
*
* @since 0.0.0
*/
public class ResourceNotFoundException extends RuntimeException {
public class ResourceNotFoundException extends ResponseStatusException {

/**
* Constructor.
*
* @param message Message
*/
public ResourceNotFoundException(final String message) {
super(message);
super(HttpStatus.NOT_FOUND, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ void handlesResourceAlreadyExistsException() {
"ResourceAlreadyExistsException isn't handled",
new AdviceController().handle(
new ResourceAlreadyExistsException(message)
),
).getStatusCode(),
new IsEqual<>(
new RestError(message, HttpStatus.BAD_REQUEST).value()
new RestError(message, HttpStatus.CONFLICT).value()
.getStatusCode()
)
);
}
Expand All @@ -92,9 +93,10 @@ void handlesResourceNotFoundException() {
"ResourceNotFoundException isn't handled",
new AdviceController().handle(
new ResourceNotFoundException(message)
),
).getStatusCode(),
new IsEqual<>(
new RestError(message, HttpStatus.NOT_FOUND).value()
.getStatusCode()
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void throwsOnInvalidProjectId() throws SQLException {
"Exception is not thrown or valid",
() -> this.projects.byId(id),
new Throws<>(
"Project %s not found".formatted(id),
"404 NOT_FOUND \"Project %s not found\"".formatted(id),
ResourceNotFoundException.class
)
).affirm();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void throwsOnInvalidKey() throws SQLException {
"Exception is not thrown",
() -> this.secrets.value(key),
new Throws<>(
"Secret with project = %s and key = %s not found"
"404 NOT_FOUND \"Secret with project = %s and key = %s not found\""
.formatted(key.getProject(), key.getName()),
ResourceNotFoundException.class
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void throwsOnDuplicate() {
"Exception is not thrown or valid",
() -> this.secrets.create(() -> expected),
new Throws<>(
"Secret with project = %s and key = %s already exists"
"409 CONFLICT \"Secret with project = %s and key = %s already exists\""
.formatted(expected.getProject(), expected.getKey()),
ResourceAlreadyExistsException.class
)
Expand All @@ -164,7 +164,7 @@ void throwsOnNonExistentSecret() {
"Exception is not thrown or valid",
() -> this.secrets.update(() -> expected),
new Throws<>(
"Secret with project = %s and key = %s not found"
"404 NOT_FOUND \"Secret with project = %s and key = %s not found\""
.formatted(expected.getProject(), expected.getKey()),
ResourceNotFoundException.class
)
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/git/tracehub/pmo/ticket/DefaultTicketsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ void throwsOnInvalidJob() throws SQLException {
"Exception is not thrown or valid",
() -> this.tickets.byJob(job, repo),
new Throws<>(
"Ticket with job = %s and repo = %s not found".formatted(job, repo),
"404 NOT_FOUND \"Ticket with job = %s and repo = %s not found\""
.formatted(job, repo),
ResourceNotFoundException.class
)
).affirm();
Expand All @@ -208,7 +209,8 @@ void throwsOnInvalidIssueNumber() throws SQLException {
"Exception is not thrown or valid",
() -> this.tickets.byNumber(number, repo),
new Throws<>(
"Ticket with issue = %s and repo = %s not found".formatted(number, repo),
"404 NOT_FOUND \"Ticket with issue = %s and repo = %s not found\""
.formatted(number, repo),
ResourceNotFoundException.class
)
).affirm();
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/it/web/CreateSecretITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void throwsOnDuplicate() throws Exception {
response.status()
),
response.status(),
new IsEqual<>(400)
new IsEqual<>(409)
);
MatcherAssert.assertThat(
"Message %s isn't correct".formatted(response.body()),
Expand All @@ -183,7 +183,7 @@ void throwsOnDuplicate() throws Exception {
new MutableJson()
.with(
"message",
"Secret with project = %s and key = %s already exists"
"409 CONFLICT \"Secret with project = %s and key = %s already exists\""
.formatted(project, key)
)
.toString()
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/it/web/RetrieveSecretByKeyITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void throwsOnInvalidKey() throws Exception {
new MutableJson()
.with(
"message",
"Secret with project = %s and key = %s not found"
"404 NOT_FOUND \"Secret with project = %s and key = %s not found\""
.formatted(project, key)
).toString()
)
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/it/web/RetrieveTicketByJobITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void throwsOnInvalidJob() throws Exception {
new MutableJson()
.with(
"message",
"Ticket with job = %s and repo = %s not found"
"404 NOT_FOUND \"Ticket with job = %s and repo = %s not found\""
.formatted(job, repo)
).toString()
)
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/it/web/RetrieveTicketByNumberITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void throwsOnInvalidNumber() throws Exception {
new MutableJson()
.with(
"message",
"Ticket with issue = %s and repo = %s not found"
"404 NOT_FOUND \"Ticket with issue = %s and repo = %s not found\""
.formatted(number, repo)
).toString()
)
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/it/web/UpdateSecretITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void throwsOnNonExistentSecret() throws Exception {
new MutableJson()
.with(
"message",
"Secret with project = %s and key = %s not found"
"404 NOT_FOUND \"Secret with project = %s and key = %s not found\""
.formatted(project, key)
)
.toString()
Expand Down
Loading