Skip to content

Commit

Permalink
Merge pull request #1612 from /issues/1611-not-found
Browse files Browse the repository at this point in the history
Fix #1611: 404 error is propagated as 500
  • Loading branch information
banterCZ authored Apr 2, 2024
2 parents 0a5ea29 + 41ed3dc commit fd9d573
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.resource.NoResourceFoundException;

/**
* Controller advice responsible for default exception resolving.
Expand Down Expand Up @@ -862,4 +863,17 @@ public DefaultExceptionResolver(Audit audit) {
return new ErrorResponse(error);
}

/**
* Exception handler for no resource found.
*
* @param e Exception.
* @return Response with error details.
*/
@ExceptionHandler(NoResourceFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public @ResponseBody ErrorResponse handleNoResourceFoundException(final NoResourceFoundException e) {
logger.warn("Error occurred when calling an API: {}", e.getMessage());
logger.debug("Exception detail: ", e);
return new ErrorResponse("ERROR_NOT_FOUND", "Resource not found.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.resource.NoResourceFoundException;

/**
* Controller advice responsible for default exception resolving.
Expand Down Expand Up @@ -141,4 +142,17 @@ public DefaultExceptionResolver(Audit audit) {
return new ErrorResponse(new TppAppError("tpp.app.unableToCreate", t.getErrors()));
}

}
/**
* Exception handler for no resource found.
*
* @param e Exception.
* @return Response with error details.
*/
@ExceptionHandler(NoResourceFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public @ResponseBody ErrorResponse handleNoResourceFoundException(final NoResourceFoundException e) {
logger.warn("Error occurred when calling an API: {}", e.getMessage());
logger.debug("Exception detail: ", e);
return new ErrorResponse("ERROR_NOT_FOUND", "Resource not found.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.resource.NoResourceFoundException;

/**
* Controller advice responsible for default exception resolving.
Expand Down Expand Up @@ -134,4 +135,17 @@ public String handleUnauthorizedException(InsufficientAuthenticationException ex
return new ErrorResponse(error);
}

/**
* Exception handler for no resource found.
*
* @param e Exception.
* @return Response with error details.
*/
@ExceptionHandler(NoResourceFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public @ResponseBody ErrorResponse handleNoResourceFoundException(final NoResourceFoundException e) {
logger.warn("Error occurred when calling an API: {}", e.getMessage());
logger.debug("Exception detail: ", e);
return new ErrorResponse("ERROR_NOT_FOUND", "Resource not found.");
}
}

0 comments on commit fd9d573

Please sign in to comment.