Best Cerberus-source code snippet using org.cerberus.api.controllers.handlers.RestExceptionHandler.RestExceptionHandler
Source:RestExceptionHandler.java
...43/**44 * @author mlombard45 */46@RestControllerAdvice(basePackages = "org.cerberus.api")47public class RestExceptionHandler {48 public RestExceptionHandler() {49 super();50 }51 @ExceptionHandler(MissingServletRequestParameterException.class)52 @ResponseStatus(HttpStatus.BAD_REQUEST)53 protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex) {54 String error = ex.getParameterName() + " parameter is missing";55 return this.buildResponseEntity(new ResponseWrapper<>(HttpStatus.BAD_REQUEST, error, ex));56 }57 @ExceptionHandler(HttpMediaTypeNotSupportedException.class)58 @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)59 protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex) {60 StringBuilder builder = new StringBuilder()61 .append(ex.getContentType())62 .append(" media type is not supported. Supported media types are ")...
RestExceptionHandler
Using AI Code Generation
1public class RestExceptionHandler {2 private static final Logger LOG = LoggerFactory.getLogger(RestExceptionHandler.class);3 @ExceptionHandler(Exception.class)4 public ResponseEntity<RestResponse> handleException(Exception ex) {5 LOG.error(ex.getMessage(), ex);6 return new ResponseEntity<>(new RestResponse(ex.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);7 }8}9public class RestResponse {10 private String message;11 private Date timestamp;12 public RestResponse(String message) {13 this.message = message;14 this.timestamp = new Date();15 }16 public String getMessage() {17 return message;18 }19 public Date getTimestamp() {20 return timestamp;21 }22}23public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {24 private static final Logger LOG = LoggerFactory.getLogger(RestResponseEntityExceptionHandler.class);25 @ExceptionHandler(Exception.class)26 public ResponseEntity<RestResponse> handleException(Exception ex) {27 LOG.error(ex.getMessage(), ex);28 return new ResponseEntity<>(new RestResponse(ex.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);29 }30}31public class RestTemplateTest {32 public static void main(String[] args) {33 RestTemplate restTemplate = new RestTemplate();34 List<Employee> employees = restTemplate.getForObject(url, List.class);35 System.out.println(employees);36 }37}38@RequestMapping("/api/v1/employees")39public class EmployeeController {40 private static final Logger LOG = LoggerFactory.getLogger(EmployeeController.class);41 public ResponseEntity<List<Employee>> getEmployees() {42 LOG.info("getEmployees()");43 List<Employee> employees = new ArrayList<>();44 employees.add(new Employee(1, "John", "
RestExceptionHandler
Using AI Code Generation
1 @ExceptionHandler(value = Exception.class)2 public ResponseEntity<RestExceptionResponse> handleException(Exception e) {3 RestExceptionResponse response = new RestExceptionResponse();4 response.setErrorMessage(e.getMessage());5 response.setErrorCode(500);6 return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);7 }8}9package org.cerberus.api.exception;10public class UserNotFoundException extends RuntimeException {11 private static final long serialVersionUID = 1L;12 public UserNotFoundException(String message) {13 super(message);14 }15}16package org.cerberus.api.controllers.handlers;17import org.cerberus.api.exception.UserNotFoundException;18import org.cerberus.api.model.response.RestExceptionResponse;19import org.springframework.http.HttpStatus;20import org.springframework.http.ResponseEntity;21import org.springframework.web.bind.annotation.ControllerAdvice;22import org.springframework.web.bind.annotation.ExceptionHandler;23import org.springframework.web.context.request.WebRequest;24import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;25public class RestExceptionHandler extends ResponseEntityExceptionHandler {26 @ExceptionHandler(value = UserNotFoundException.class)27 public ResponseEntity<RestExceptionResponse> handleUserNotFoundException(UserNotFoundException e, WebRequest request) {28 RestExceptionResponse response = new RestExceptionResponse();29 response.setErrorMessage(e.getMessage());30 response.setErrorCode(404);31 return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);32 }33 @ExceptionHandler(value = Exception.class)34 public ResponseEntity<RestExceptionResponse> handleException(Exception e) {35 RestExceptionResponse response = new RestExceptionResponse();36 response.setErrorMessage(e.getMessage());37 response.setErrorCode(500);38 return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER
RestExceptionHandler
Using AI Code Generation
1@ExceptionHandler(RestAPIException.class)2public ResponseEntity<RestResponse> handleRestAPIException(RestAPIException exception) {3 return RestExceptionHandler.handleRestAPIException(exception);4}5This is the default implementation of RestExceptionHandler.handleRestAPIException() method:6public static ResponseEntity<RestResponse> handleRestAPIException(RestAPIException exception) {7 RestResponse restResponse = new RestResponse();8 restResponse.setHttpCode(exception.getHttpCode());9 restResponse.setHttpMessage(exception.getHttpMessage());10 restResponse.setErrorCode(exception.getErrorCode());11 restResponse.setErrorMessage(exception.getErrorMessage());12 restResponse.setMoreInfoUrl(exception.getMoreInfoUrl());13 return new ResponseEntity<RestResponse>(restResponse, HttpStatus.valueOf(exception.getHttpCode()));14}15public class RestAPIException extends Exception {16 private static final long serialVersionUID = 1L;17 private int httpCode;18 private String httpMessage;19 private int errorCode;20 private String errorMessage;21 private String moreInfoUrl;22 public RestAPIException(int httpCode, String httpMessage, int errorCode, String errorMessage, String moreInfoUrl) {23 this.httpCode = httpCode;24 this.httpMessage = httpMessage;25 this.errorCode = errorCode;26 this.errorMessage = errorMessage;27 this.moreInfoUrl = moreInfoUrl;28 }29 public int getHttpCode() {30 return httpCode;31 }32 public void setHttpCode(int httpCode) {33 this.httpCode = httpCode;34 }35 public String getHttpMessage() {36 return httpMessage;37 }38 public void setHttpMessage(String httpMessage) {39 this.httpMessage = httpMessage;40 }41 public int getErrorCode() {42 return errorCode;43 }44 public void setErrorCode(int errorCode) {45 this.errorCode = errorCode;46 }47 public String getErrorMessage() {48 return errorMessage;49 }50 public void setErrorMessage(String errorMessage) {51 this.errorMessage = errorMessage;52 }53 public String getMoreInfoUrl() {54 return moreInfoUrl;55 }56 public void setMoreInfoUrl(String moreInfoUrl) {57 this.moreInfoUrl = moreInfoUrl;58 }59}60public class RestResponse {61 private int httpCode;62 private String httpMessage;63 private int errorCode;64 private String errorMessage;65 private String moreInfoUrl;
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!