Search in sources :

Example 1 with UnsatisfiedArgumentException

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);
    }
}
Also used : UnsatisfiedArgumentException(io.micronaut.core.bind.exceptions.UnsatisfiedArgumentException) BoundExecutable(io.micronaut.core.bind.BoundExecutable) UnsatisfiedArgumentException(io.micronaut.core.bind.exceptions.UnsatisfiedArgumentException) CodecException(io.micronaut.http.codec.CodecException) IOException(java.io.IOException)

Example 2 with UnsatisfiedArgumentException

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;
        }
    };
}
Also used : UnsatisfiedArgumentException(io.micronaut.core.bind.exceptions.UnsatisfiedArgumentException) Argument(io.micronaut.core.type.Argument) ConversionError(io.micronaut.core.convert.ConversionError) ConversionErrorException(io.micronaut.core.convert.exceptions.ConversionErrorException) ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext)

Aggregations

UnsatisfiedArgumentException (io.micronaut.core.bind.exceptions.UnsatisfiedArgumentException)2 BoundExecutable (io.micronaut.core.bind.BoundExecutable)1 ArgumentConversionContext (io.micronaut.core.convert.ArgumentConversionContext)1 ConversionError (io.micronaut.core.convert.ConversionError)1 ConversionErrorException (io.micronaut.core.convert.exceptions.ConversionErrorException)1 Argument (io.micronaut.core.type.Argument)1 CodecException (io.micronaut.http.codec.CodecException)1 IOException (java.io.IOException)1