org.springframework.hateoas.VndErrors Java Examples

The following examples show how to use org.springframework.hateoas.VndErrors. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TaskLauncherTaskletTests.java    From composed-task-runner with Apache License 2.0 6 votes vote down vote up
@Test
@DirtiesContext
public void testInvalidTaskName() {
	final String ERROR_MESSAGE =
			"Could not find task definition named " + TASK_NAME;
	VndErrors errors = new VndErrors("message", ERROR_MESSAGE, new Link("ref"));
	Mockito.doThrow(new DataFlowClientException(errors))
			.when(this.taskOperations)
			.launch(ArgumentMatchers.anyString(),
					ArgumentMatchers.any(),
					ArgumentMatchers.any(), ArgumentMatchers.any());
	TaskLauncherTasklet taskLauncherTasklet = getTaskExecutionTasklet();
	ChunkContext chunkContext = chunkContext();
	Throwable exception = assertThrows(DataFlowClientException.class,
			() -> taskLauncherTasklet.execute(null, chunkContext));
	Assertions.assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE);
}
 
Example #2
Source File: RestControllerAdvice.java    From spring-cloud-dashboard with Apache License 2.0 6 votes vote down vote up
/**
 * Log the exception message at warn level and stack trace as trace level.
 * Return response status HttpStatus.NOT_FOUND
 */
@ExceptionHandler({NoSuchAppRegistrationException.class,
		NoSuchTaskDefinitionException.class,
		NoSuchTaskExecutionException.class,
		NoSuchJobExecutionException.class,
		NoSuchJobInstanceException.class,
		NoSuchJobException.class,
		NoSuchStepExecutionException.class,
		MetricsMvcEndpoint.NoSuchMetricException.class})
@ResponseStatus(HttpStatus.NOT_FOUND)
@ResponseBody
public VndErrors onNotFoundException(Exception e) {
	String logref = logWarnLevelExceptionMessage(e);
	if (logger.isTraceEnabled()) {
		logTraceLevelStrackTrace(e);
	}
	String msg = getExceptionMessage(e);
	return new VndErrors(logref, msg);
}
 
Example #3
Source File: RestControllerAdvice.java    From spring-cloud-dashboard with Apache License 2.0 6 votes vote down vote up
/**
 * Client did not formulate a correct request.
 * Log the exception message at warn level and stack trace as trace level.
 * Return response status HttpStatus.BAD_REQUEST (400).
 */
@ExceptionHandler({
	MissingServletRequestParameterException.class,
	MethodArgumentTypeMismatchException.class,
	InvalidStreamDefinitionException.class
})
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public VndErrors onClientGenericBadRequest(Exception e) {
	String logref = logWarnLevelExceptionMessage(e);
	if (logger.isTraceEnabled()) {
		logTraceLevelStrackTrace(e);
	}
	String msg = getExceptionMessage(e);
	return new VndErrors(logref, msg);
}
 
Example #4
Source File: RestControllerAdvice.java    From spring-cloud-dashboard with Apache License 2.0 6 votes vote down vote up
/**
 * The exception handler is trigger if a JSR303 {@link ConstraintViolationException}
 * is being raised.
 *
 * Log the exception message at warn level and stack trace as trace level.
 * Return response status HttpStatus.BAD_REQUEST (400).
 */
@ExceptionHandler({ConstraintViolationException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public VndErrors onConstraintViolationException(ConstraintViolationException e) {
	String logref = logWarnLevelExceptionMessage(e);
	if (logger.isTraceEnabled()) {
		logTraceLevelStrackTrace(e);
	}

	final StringBuilder errorMessage = new StringBuilder();
	boolean first = true;
	for (ConstraintViolation<?> violation : e.getConstraintViolations()) {
		if (!first) {
			errorMessage.append("; ");
		}
		errorMessage.append(violation.getMessage());
		first = false;
	}

	return new VndErrors(logref, errorMessage.toString());
}
 
Example #5
Source File: DataFlowClientException.java    From spring-cloud-dashboard with Apache License 2.0 5 votes vote down vote up
@Override
public String getMessage() {
	StringBuilder builder = new StringBuilder();
	for (VndErrors.VndError e : vndErrors) {
		builder.append(e.getMessage()).append('\n');
	}
	return builder.toString();
}
 
Example #6
Source File: VndErrorResponseErrorHandler.java    From spring-cloud-dashboard with Apache License 2.0 5 votes vote down vote up
@Override
public void handleError(ClientHttpResponse response) throws IOException {
	VndErrors error = null;
	try {
		error = errorExtractor.extractData(response);
	}
	catch (Exception e) {
		super.handleError(response);
	}
	throw new DataFlowClientException(error);
}
 
Example #7
Source File: RestControllerAdvice.java    From spring-cloud-dashboard with Apache License 2.0 5 votes vote down vote up
/**
 * Handles the general error case. Log track trace at error level
 */
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public VndErrors onException(Exception e) {
	logger.error("Caught exception while handling a request", e);
	String logref = e.getClass().getSimpleName();
	String msg = getExceptionMessage(e);
	return new VndErrors(logref, msg);
}
 
Example #8
Source File: RestControllerAdvice.java    From spring-cloud-dashboard with Apache License 2.0 5 votes vote down vote up
/**
 * Log the exception message at warn level and stack trace as trace level.
 * Return response status HttpStatus.CONFLICT
 */
@ExceptionHandler({
		AppAlreadyRegisteredException.class,
		DuplicateTaskException.class})
@ResponseStatus(HttpStatus.CONFLICT)
@ResponseBody
public VndErrors onConflictException(Exception e) {
	String logref = logWarnLevelExceptionMessage(e);
	if (logger.isTraceEnabled()) {
		logTraceLevelStrackTrace(e);
	}
	String msg = getExceptionMessage(e);
	return new VndErrors(logref, msg);
}
 
Example #9
Source File: ExceptionAdvice.java    From spring-ddd-bank with GNU Lesser General Public License v2.1 5 votes vote down vote up
@ResponseBody
@ExceptionHandler({ Exception.class })
/**
 * Reports the given Exception with messages in three ways:
 * <ol>
 * <li>with messages in default language and with stack trace to the error
 * log</li>
 * <li>with localized messages according to the given Locale of the web request
 * to the REST client</li>
 * <li>as HTTP status code to the REST client</li>
 * </ol>
 */
VndErrors reportException(final Exception ex, final Locale requestLocale, final HttpServletResponse response) {
	// prepare messages for REST client with the Locale of the request:
	/** Message texts for exceptions. */
	final ResourceBundle requestResourceBundle = ResourceBundle.getBundle(BASE_NAME, requestLocale);
	final StringBuffer clientMessages = new StringBuffer();
	multex.Msg.printMessages(clientMessages, ex, requestResourceBundle);
	final String clientMesagesString = clientMessages.toString();

	// prepare log report with messages and stack trace:
	final StringBuffer serverMessages = new StringBuffer();
	serverMessages.append("Processing REST request threw exception:\n");
	final Locale defaultLocale = Locale.getDefault();
	final ResourceBundle defaultResourceBundle = ResourceBundle.getBundle(BASE_NAME, defaultLocale);
	if (!defaultResourceBundle.equals(requestResourceBundle)) {
		serverMessages.append(clientMesagesString);
		serverMessages.append("\n-----\n");
	}
	Msg.printReport(serverMessages, ex, defaultResourceBundle);

	// log the report on the server:
	log.error(serverMessages.toString());
	// respond with localized messages to the client:
	response.setStatus(exceptionToStatus(ex).value());
	return new VndErrors("error", clientMesagesString);
}
 
Example #10
Source File: DataFlowClientException.java    From spring-cloud-dashboard with Apache License 2.0 4 votes vote down vote up
public DataFlowClientException(VndErrors error) {
	this.vndErrors = error;
}
 
Example #11
Source File: VndErrorResponseErrorHandler.java    From spring-cloud-dashboard with Apache License 2.0 4 votes vote down vote up
public VndErrorResponseErrorHandler(List<HttpMessageConverter<?>> messageConverters) {
	errorExtractor = new HttpMessageConverterExtractor<VndErrors>(VndErrors.class, messageConverters);
}
 
Example #12
Source File: PersonControllerAdvice.java    From building-microservices with Apache License 2.0 4 votes vote down vote up
@ResponseStatus(value = HttpStatus.NOT_FOUND)
@ExceptionHandler(FileNotFoundException.class)
public VndErrors fileNotFoundException(FileNotFoundException ex) {
	return this.error(ex, ex.getLocalizedMessage());
}
 
Example #13
Source File: PersonControllerAdvice.java    From building-microservices with Apache License 2.0 4 votes vote down vote up
@ResponseStatus(value = HttpStatus.NOT_FOUND)
@ExceptionHandler(PersonNotFoundException.class)
public VndErrors personNotFoundException(PersonNotFoundException e) {
	return this.error(e, e.getPersonId() + "");
}
 
Example #14
Source File: PersonControllerAdvice.java    From building-microservices with Apache License 2.0 4 votes vote down vote up
private <E extends Exception> VndErrors error(E e, String logref) {
	String msg = Optional.of(e.getMessage()).orElse(e.getClass().getSimpleName());
	return new VndErrors(logref, msg);
}