org.springframework.web.server.ServerErrorException Java Examples

The following examples show how to use org.springframework.web.server.ServerErrorException. 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: SyncInvocableHandlerMethod.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Invoke the method for the given exchange.
 * @param exchange the current exchange
 * @param bindingContext the binding context to use
 * @param providedArgs optional list of argument values to match by type
 * @return a Mono with a {@link HandlerResult}.
 * @throws ServerErrorException if method argument resolution or method invocation fails
 */
@Nullable
public HandlerResult invokeForHandlerResult(ServerWebExchange exchange,
		BindingContext bindingContext, Object... providedArgs) {

	MonoProcessor<HandlerResult> processor = MonoProcessor.create();
	this.delegate.invoke(exchange, bindingContext, providedArgs).subscribeWith(processor);

	if (processor.isTerminated()) {
		Throwable ex = processor.getError();
		if (ex != null) {
			throw (ex instanceof ServerErrorException ? (ServerErrorException) ex :
					new ServerErrorException("Failed to invoke: " + getShortLogMessage(), getMethod(), ex));
		}
		return processor.peek();
	}
	else {
		// Should never happen...
		throw new IllegalStateException(
				"SyncInvocableHandlerMethod should have completed synchronously.");
	}
}
 
Example #2
Source File: SyncInvocableHandlerMethod.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Invoke the method for the given exchange.
 * @param exchange the current exchange
 * @param bindingContext the binding context to use
 * @param providedArgs optional list of argument values to match by type
 * @return a Mono with a {@link HandlerResult}.
 * @throws ServerErrorException if method argument resolution or method invocation fails
 */
@Nullable
public HandlerResult invokeForHandlerResult(ServerWebExchange exchange,
		BindingContext bindingContext, Object... providedArgs) {

	MonoProcessor<HandlerResult> processor = MonoProcessor.create();
	this.delegate.invoke(exchange, bindingContext, providedArgs).subscribeWith(processor);

	if (processor.isTerminated()) {
		Throwable ex = processor.getError();
		if (ex != null) {
			throw (ex instanceof ServerErrorException ? (ServerErrorException) ex :
					new ServerErrorException("Failed to invoke: " + getShortLogMessage(), getMethod(), ex));
		}
		return processor.peek();
	}
	else {
		// Should never happen...
		throw new IllegalStateException(
				"SyncInvocableHandlerMethod should have completed synchronously.");
	}
}
 
Example #3
Source File: BackstopperSpringWebFluxComponentTest.java    From backstopper with Apache License 2.0 6 votes vote down vote up
@Test
public void verify_GENERIC_SERVICE_ERROR_returned_if_ServerErrorException_is_thrown() {
    ExtractableResponse response =
        given()
            .baseUri("http://localhost")
            .port(SERVER_PORT)
            .log().all()
            .when()
            .get(SERVER_ERROR_EXCEPTION_ENDPOINT_PATH, "doesNotMatter")
            .then()
            .log().all()
            .extract();

    verifyErrorReceived(response, SampleCoreApiError.GENERIC_SERVICE_ERROR);
    ServerErrorException ex = verifyResponseStatusExceptionSeenByBackstopper(ServerErrorException.class, 500);
    verifyHandlingResult(
        SampleCoreApiError.GENERIC_SERVICE_ERROR,
        Pair.of("exception_message", quotesToApostrophes(ex.getMessage())),
        Pair.of("method_parameter", ex.getMethodParameter().toString()),
        Pair.of("handler_method", ex.getHandlerMethod().toString())
    );
}
 
Example #4
Source File: MatrixVariablesMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test(expected = ServerErrorException.class)
public void resolveArgumentMultipleMatches() throws Exception {
	getVariablesFor("var1").add("colors", "red");
	getVariablesFor("var2").add("colors", "green");

	MethodParameter param = this.testMethod.annot(matrixAttribute().noName()).arg(List.class, String.class);
	this.resolver.resolveArgument(param, new BindingContext(), this.exchange).block(Duration.ZERO);
}
 
Example #5
Source File: PathVariableMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void handleMissingValue() {
	BindingContext bindingContext = new BindingContext();
	Mono<Object> mono = this.resolver.resolveArgument(this.paramNamedString, bindingContext, this.exchange);
	StepVerifier.create(mono)
			.expectNextCount(0)
			.expectError(ServerErrorException.class)
			.verify();
}
 
Example #6
Source File: MatrixVariablesMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test(expected = ServerErrorException.class)
public void resolveArgumentMultipleMatches() throws Exception {
	getVariablesFor("var1").add("colors", "red");
	getVariablesFor("var2").add("colors", "green");

	MethodParameter param = this.testMethod.annot(matrixAttribute().noName()).arg(List.class, String.class);
	this.resolver.resolveArgument(param, new BindingContext(), this.exchange).block(Duration.ZERO);
}
 
Example #7
Source File: PathVariableMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void handleMissingValue() throws Exception {
	BindingContext bindingContext = new BindingContext();
	Mono<Object> mono = this.resolver.resolveArgument(this.paramNamedString, bindingContext, this.exchange);
	StepVerifier.create(mono)
			.expectNextCount(0)
			.expectError(ServerErrorException.class)
			.verify();
}
 
Example #8
Source File: OneOffSpringWebFluxFrameworkExceptionHandlerListenerTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@DataProvider(value = {
    "true",
    "false"
})
@Test
public void handleFluxExceptions_handles_ServerErrorException_as_expected(
    boolean nullDetails
) throws NoSuchMethodException {
    // given
    MethodParameter details = new MethodParameter(String.class.getDeclaredMethod("length"), -1);

    ServerErrorException ex =
        (nullDetails)
        ? new ServerErrorException("Some reason", (Throwable) null)
        : new ServerErrorException("Some reason", details, null);

    List<Pair<String, String>> expectedExtraDetailsForLogging = new ArrayList<>();
    ApiExceptionHandlerUtils.DEFAULT_IMPL.addBaseExceptionMessageToExtraDetailsForLogging(
        ex, expectedExtraDetailsForLogging
    );

    expectedExtraDetailsForLogging.add(
        Pair.of("method_parameter", String.valueOf(ex.getMethodParameter()))
    );
    expectedExtraDetailsForLogging.add(
        Pair.of("handler_method", String.valueOf(ex.getHandlerMethod()))
    );

    // when
    ApiExceptionHandlerListenerResult result = listener.handleSpringMvcOrWebfluxSpecificFrameworkExceptions(ex);

    // then
    validateResponse(
        result,
        true,
        singleton(testProjectApiErrors.getGenericServiceError()),
        expectedExtraDetailsForLogging
    );
}
 
Example #9
Source File: MatrixVariableMethodArgumentResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Nullable
@Override
protected Object resolveNamedValue(String name, MethodParameter param, ServerWebExchange exchange) {
	Map<String, MultiValueMap<String, String>> pathParameters =
			exchange.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE);
	if (CollectionUtils.isEmpty(pathParameters)) {
		return null;
	}

	MatrixVariable ann = param.getParameterAnnotation(MatrixVariable.class);
	Assert.state(ann != null, "No MatrixVariable annotation");
	String pathVar = ann.pathVar();
	List<String> paramValues = null;

	if (!pathVar.equals(ValueConstants.DEFAULT_NONE)) {
		if (pathParameters.containsKey(pathVar)) {
			paramValues = pathParameters.get(pathVar).get(name);
		}
	}
	else {
		boolean found = false;
		paramValues = new ArrayList<>();
		for (MultiValueMap<String, String> params : pathParameters.values()) {
			if (params.containsKey(name)) {
				if (found) {
					String paramType = param.getNestedParameterType().getName();
					throw new ServerErrorException(
							"Found more than one match for URI path parameter '" + name +
							"' for parameter type [" + paramType + "]. Use 'pathVar' attribute to disambiguate.",
							param, null);
				}
				paramValues.addAll(params.get(name));
				found = true;
			}
		}
	}

	if (CollectionUtils.isEmpty(paramValues)) {
		return null;
	}
	else if (paramValues.size() == 1) {
		return paramValues.get(0);
	}
	else {
		return paramValues;
	}
}
 
Example #10
Source File: PathVariableMethodArgumentResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected void handleMissingValue(String name, MethodParameter parameter) {
	throw new ServerErrorException(name, parameter, null);
}
 
Example #11
Source File: MatrixVariableMethodArgumentResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Nullable
@Override
protected Object resolveNamedValue(String name, MethodParameter param, ServerWebExchange exchange) {
	Map<String, MultiValueMap<String, String>> pathParameters =
			exchange.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE);
	if (CollectionUtils.isEmpty(pathParameters)) {
		return null;
	}

	MatrixVariable ann = param.getParameterAnnotation(MatrixVariable.class);
	Assert.state(ann != null, "No MatrixVariable annotation");
	String pathVar = ann.pathVar();
	List<String> paramValues = null;

	if (!pathVar.equals(ValueConstants.DEFAULT_NONE)) {
		if (pathParameters.containsKey(pathVar)) {
			paramValues = pathParameters.get(pathVar).get(name);
		}
	}
	else {
		boolean found = false;
		paramValues = new ArrayList<>();
		for (MultiValueMap<String, String> params : pathParameters.values()) {
			if (params.containsKey(name)) {
				if (found) {
					String paramType = param.getNestedParameterType().getName();
					throw new ServerErrorException(
							"Found more than one match for URI path parameter '" + name +
							"' for parameter type [" + paramType + "]. Use 'pathVar' attribute to disambiguate.",
							param, null);
				}
				paramValues.addAll(params.get(name));
				found = true;
			}
		}
	}

	if (CollectionUtils.isEmpty(paramValues)) {
		return null;
	}
	else if (paramValues.size() == 1) {
		return paramValues.get(0);
	}
	else {
		return paramValues;
	}
}
 
Example #12
Source File: PathVariableMethodArgumentResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected void handleMissingValue(String name, MethodParameter parameter) {
	throw new ServerErrorException(name, parameter, null);
}
 
Example #13
Source File: ExceptionAdviceHandler.java    From oauth-boot with MIT License 4 votes vote down vote up
@ExceptionHandler(value = ServerErrorException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public BaseResponse serverErrorExceptionHandler(HttpServletResponse response) {
    return this.serverErrorHandler();
}