How to use RestExceptionHandler class of org.cerberus.api.controllers.handlers package

Best Cerberus-source code snippet using org.cerberus.api.controllers.handlers.RestExceptionHandler

Source:RestExceptionHandler.java Github

copy

Full Screen

...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 ")...

Full Screen

Full Screen

RestExceptionHandler

Using AI Code Generation

copy

Full Screen

1import org.cerberus.api.controllers.handlers.RestExceptionHandler;2import org.springframework.context.annotation.Bean;3import org.springframework.context.annotation.Configuration;4import org.springframework.web.servlet.HandlerExceptionResolver;5import org.springframework.web.servlet.config.annotation.EnableWebMvc;6import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;7import java.util.List;8public class WebConfig extends WebMvcConfigurerAdapter {9 public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {10 exceptionResolvers.add(restExceptionHandler());11 }12 public RestExceptionHandler restExceptionHandler() {13 return new RestExceptionHandler();14 }15}16package org.cerberus.api.controllers.handlers;17import org.cerberus.api.controllers.exceptions.ApiException;18import org.cerberus.api.controllers.exceptions.ApiExceptionResponse;19import org.cerberus.api.controllers.exceptions.ApiExceptionResponse.ApiError;20import org.springframework.http.HttpStatus;21import org.springframework.http.ResponseEntity;22import org.springframework.web.bind.annotation.ControllerAdvice;23import org.springframework.web.bind.annotation.ExceptionHandler;24import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;25public class RestExceptionHandler extends ResponseEntityExceptionHandler {26 @ExceptionHandler(ApiException.class)27 public ResponseEntity<ApiExceptionResponse> handleApiException(ApiException ex) {28 ApiError error = new ApiError(ex.getErrorCode(), ex.getMessage());29 ApiExceptionResponse response = new ApiExceptionResponse(error);30 return new ResponseEntity<>(response, ex.getHttpStatus());31 }32}

Full Screen

Full Screen

RestExceptionHandler

Using AI Code Generation

copy

Full Screen

1import org.cerberus.api.controllers.handlers.RestExceptionHandler;2import org.springframework.context.annotation.Configuration;3import org.springframework.context.annotation.Import;4import org.springframework.web.servlet.config.annotation.EnableWebMvc;5import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;6@Import(RestExceptionHandler.class)7public class WebConfig extends WebMvcConfigurerAdapter {8}9public class GlobalExceptionHandler {10 @ExceptionHandler(Exception.class)11 public ModelAndView handleException(HttpServletRequest request, Exception ex){12 ModelAndView mv = new ModelAndView();13 mv.addObject("exception", ex);14 mv.addObject("url", request.getRequestURL());15 mv.setViewName("error");16 return mv;17 }18}19public class GlobalExceptionHandler {20 @ExceptionHandler(Exception.class)21 public ModelAndView handleException(HttpServletRequest request, Exception ex){22 ModelAndView mv = new ModelAndView();23 mv.addObject("exception", ex);24 mv.addObject("url", request.getRequestURL());25 mv.setViewName("error");26 return mv;27 }28 @ExceptionHandler(NullPointerException.class)29 public ModelAndView handleNullPointerException(HttpServletRequest request, Exception ex){30 ModelAndView mv = new ModelAndView();31 mv.addObject("exception", ex);32 mv.addObject("url", request.getRequestURL());33 mv.setViewName("error");34 return mv;35 }36}

Full Screen

Full Screen

RestExceptionHandler

Using AI Code Generation

copy

Full Screen

1import org.cerberus.api.controllers.handlers.RestExceptionHandler;2import org.springframework.context.annotation.Bean;3import org.springframework.context.annotation.Configuration;4import org.springframework.http.converter.HttpMessageConverter;5import org.springframework.web.servlet.config.annotation.EnableWebMvc;6import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;7public class RestConfiguration extends WebMvcConfigurerAdapter {8 public RestExceptionHandler exceptionHandler() {9 return new RestExceptionHandler();10 }11 public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {12 converters.add(new MappingJackson2HttpMessageConverter());13 }14}

Full Screen

Full Screen

RestExceptionHandler

Using AI Code Generation

copy

Full Screen

1package org.cerberus.api.controllers.handlers;2import org.springframework.http.HttpStatus;3import org.springframework.http.ResponseEntity;4import org.springframework.web.bind.annotation.ControllerAdvice;5import org.springframework.web.bind.annotation.ExceptionHandler;6import com.cerberus.api.controllers.exceptions.InvalidRequestException;7import com.cerberus.api.controllers.exceptions.ResourceNotFoundException;8import com.cerberus.api.controllers.exceptions.UnauthorizedException;9public class RestExceptionHandler {10 @ExceptionHandler(ResourceNotFoundException.class)11 public ResponseEntity<RestError> handleResourceNotFoundException(ResourceNotFoundException e) {12 RestError error = new RestError(HttpStatus.NOT_FOUND.value(), e.getMessage());13 return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);14 }15 @ExceptionHandler(InvalidRequestException.class)16 public ResponseEntity<RestError> handleInvalidRequestException(InvalidRequestException e) {17 RestError error = new RestError(HttpStatus.BAD_REQUEST.value(), e.getMessage());18 return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);19 }20 @ExceptionHandler(UnauthorizedException.class)21 public ResponseEntity<RestError> handleUnauthorizedException(UnauthorizedException e) {22 RestError error = new RestError(HttpStatus.UNAUTHORIZED.value(), e.getMessage());23 return new ResponseEntity<>(error, HttpStatus.UNAUTHORIZED);24 }25}

Full Screen

Full Screen

RestExceptionHandler

Using AI Code Generation

copy

Full Screen

1import org.cerberus.api.controllers.handlers.RestExceptionHandler;2import org.cerberus.api.controllers.handlers.RestResponse;3import org.springframework.beans.factory.annotation.Autowired;4import org.springframework.http.HttpStatus;5import org.springframework.web.bind.annotation.RequestMapping;6import org.springframework.web.bind.annotation.RequestMethod;7import org.springframework.web.bind.annotation.RestController;8@RequestMapping("/api/v1/health")9public class HealthController {10 private RestExceptionHandler restExceptionHandler;11 @RequestMapping(method = RequestMethod.GET)12 public RestResponse getHealth() {13 return restExceptionHandler.handleResponse(HttpStatus.OK, "OK");14 }15}16package org.cerberus.api.controllers;17import org.cerberus.api.AbstractTest;18import org.cerberus.api.model.Health;19import org.junit.Test;20import org.springframework.beans.factory.annotation.Autowired;21import org.springframework.http.MediaType;22import org.springframework.test.web.servlet.ResultActions;23import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;24import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;25public class HealthControllerTest extends AbstractTest {26 private HealthController healthController;27 public void testGetHealth() throws Exception {28 String uri = "/api/v1/health";29 ResultActions resultActions = mvc.perform(MockMvcRequestBuilders.get(uri)30 .accept(MediaType.APPLICATION_JSON));31 resultActions.andExpect(status().isOk());32 resultActions.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));33 resultActions.andExpect(jsonPath("$.status").value("OK"));34 resultActions.andExpect(jsonPath("$.message").value("OK"));35 }36}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful