use of io.micronaut.core.bind.exceptions.UnsatisfiedArgumentException in project micronaut-core by micronaut-projects.
the class AbstractNettyWebSocketHandler method forwardErrorToUser.
private void forwardErrorToUser(ChannelHandlerContext ctx, Consumer<Throwable> fallback, Throwable cause) {
Optional<? extends MethodExecutionHandle<?, ?>> opt = webSocketBean.errorMethod();
if (opt.isPresent()) {
MethodExecutionHandle<?, ?> errorMethod = opt.get();
try {
BoundExecutable boundExecutable = bindMethod(originatingRequest, webSocketBinder, errorMethod, Collections.singletonList(cause));
Object target = errorMethod.getTarget();
Object result;
try {
result = boundExecutable.invoke(target);
} catch (Exception e) {
if (LOG.isErrorEnabled()) {
LOG.error("Error invoking to @OnError handler " + target.getClass().getSimpleName() + "." + errorMethod.getExecutableMethod() + ": " + e.getMessage(), e);
}
fallback.accept(e);
return;
}
if (Publishers.isConvertibleToPublisher(result)) {
Flux<?> flowable = Flux.from(instrumentPublisher(ctx, result));
flowable.collectList().subscribe(objects -> fallback.accept(cause), throwable -> {
if (throwable != null && LOG.isErrorEnabled()) {
LOG.error("Error subscribing to @OnError handler " + target.getClass().getSimpleName() + "." + errorMethod.getExecutableMethod() + ": " + throwable.getMessage(), throwable);
}
fallback.accept(cause);
});
}
} catch (UnsatisfiedArgumentException e) {
fallback.accept(cause);
}
} else {
fallback.accept(cause);
}
}
use of io.micronaut.core.bind.exceptions.UnsatisfiedArgumentException in project micronaut-core by micronaut-projects.
the class DefaultExecutableBinder method bind.
@Override
public <T, R> BoundExecutable<T, R> bind(Executable<T, R> target, ArgumentBinderRegistry<S> registry, S source) throws UnsatisfiedArgumentException {
Argument[] arguments = target.getArguments();
Object[] boundArguments = new Object[arguments.length];
for (int i = 0; i < arguments.length; i++) {
Argument<?> argument = arguments[i];
if (preBound.containsKey(argument)) {
boundArguments[i] = preBound.get(argument);
} else {
Optional<? extends ArgumentBinder<?, S>> argumentBinder = registry.findArgumentBinder(argument, source);
if (argumentBinder.isPresent()) {
ArgumentBinder<?, S> binder = argumentBinder.get();
ArgumentConversionContext conversionContext = ConversionContext.of(argument);
ArgumentBinder.BindingResult<?> bindingResult = binder.bind(conversionContext, source);
if (!bindingResult.isPresentAndSatisfied()) {
if (argument.isNullable()) {
boundArguments[i] = null;
} else {
final Optional<ConversionError> lastError = conversionContext.getLastError();
if (lastError.isPresent()) {
throw new ConversionErrorException(argument, lastError.get());
} else {
throw new UnsatisfiedArgumentException(argument);
}
}
} else {
boundArguments[i] = bindingResult.get();
}
} else {
throw new UnsatisfiedArgumentException(argument);
}
}
}
return new BoundExecutable<T, R>() {
@Override
public Executable<T, R> getTarget() {
return target;
}
@Override
public R invoke(T instance) {
return target.invoke(instance, getBoundArguments());
}
@Override
public Object[] getBoundArguments() {
return boundArguments;
}
};
}
Aggregations