use of org.eclipse.microprofile.graphql.GraphQLException in project smallrye-graphql by smallrye.
the class ReflectionInvoker method invoke.
public <T> T invoke(Object... arguments) throws Exception {
if (this.injectContextAt > -1) {
arguments = injectContext(arguments);
}
try {
ManagedInstance<?> operationInstance = lookupService.getInstance(operationClass);
Object operationInstance1 = operationInstance.get();
eventEmitter.fireBeforeMethodInvoke(new InvokeInfo(operationInstance1, method, arguments));
T result = (T) method.invoke(operationInstance1, arguments);
if (result instanceof Uni) {
return (T) ((Uni) result).onTermination().invoke(() -> {
operationInstance.destroyIfNecessary();
});
} else if (result instanceof Multi) {
return (T) ((Multi) result).onTermination().invoke(() -> {
operationInstance.destroyIfNecessary();
});
} else {
operationInstance.destroyIfNecessary();
return result;
}
} catch (InvocationTargetException ex) {
// Invoked method has thrown something, unwrap
Throwable throwable = ex.getCause();
if (throwable instanceof Error) {
throw (Error) throwable;
} else if (throwable instanceof GraphQLException) {
throw (GraphQLException) throwable;
} else if (throwable instanceof Exception) {
throw (Exception) throwable;
} else {
throw msg.generalDataFetcherException(operationClass.getName() + ": " + method.getName(), throwable);
}
}
}
use of org.eclipse.microprofile.graphql.GraphQLException in project smallrye-graphql by smallrye.
the class AbstractDataFetcher method get.
@Override
public T get(final DataFetchingEnvironment dfe) throws Exception {
// update the context
SmallRyeContext context = initSmallRyeContext(dfe);
final DataFetcherResult.Builder<Object> resultBuilder = DataFetcherResult.newResult().localContext(dfe.getGraphQlContext());
try {
Object[] transformedArguments = argumentHelper.getArguments(dfe);
return invokeAndTransform(dfe, resultBuilder, transformedArguments);
} catch (AbstractDataFetcherException abstractDataFetcherException) {
// Arguments or result couldn't be transformed
abstractDataFetcherException.appendDataFetcherResult(resultBuilder, dfe);
eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), abstractDataFetcherException);
} catch (GraphQLException graphQLException) {
errorResultHelper.appendPartialResult(resultBuilder, dfe, graphQLException);
eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), graphQLException);
} catch (Throwable ex) {
eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), ex);
throw ex;
} finally {
eventEmitter.fireAfterDataFetch(context);
}
return invokeFailure(resultBuilder);
}
use of org.eclipse.microprofile.graphql.GraphQLException in project smallrye-graphql by smallrye.
the class CompletionStageDataFetcher method invokeAndTransform.
@Override
protected <T> T invokeAndTransform(DataFetchingEnvironment dfe, DataFetcherResult.Builder<Object> resultBuilder, Object[] transformedArguments) throws AbstractDataFetcherException, Exception {
SmallRyeContext context = contextHelper.getSmallRyeContext(dfe);
ThreadContext threadContext = ThreadContext.builder().build();
SmallRyeContext.setContext(context);
try {
CompletionStage<Object> futureResultFromMethodCall = threadContext.withContextCapture(operationInvoker.invoke(transformedArguments));
return (T) futureResultFromMethodCall.handle((result, throwable) -> {
if (throwable != null) {
eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), throwable);
if (throwable instanceof GraphQLException) {
GraphQLException graphQLException = (GraphQLException) throwable;
errorResultHelper.appendPartialResult(resultBuilder, dfe, graphQLException);
} else if (throwable instanceof Exception) {
throw SmallRyeGraphQLServerMessages.msg.dataFetcherException(operation, throwable);
} else if (throwable instanceof Error) {
throw ((Error) throwable);
}
} else {
try {
resultBuilder.data(fieldHelper.transformOrAdaptResponse(result, dfe));
} catch (AbstractDataFetcherException te) {
te.appendDataFetcherResult(resultBuilder, dfe);
}
}
return resultBuilder.build();
});
} finally {
SmallRyeContext.remove();
}
}
use of org.eclipse.microprofile.graphql.GraphQLException in project smallrye-graphql by smallrye.
the class MultiDataFetcher method invokeAndTransform.
@Override
@SuppressWarnings("unchecked")
protected <O> O invokeAndTransform(DataFetchingEnvironment dfe, DataFetcherResult.Builder<Object> resultBuilder, Object[] transformedArguments) throws Exception {
SmallRyeContext context = contextHelper.getSmallRyeContext(dfe);
try {
SmallRyeContext.setContext(context);
Multi<?> multi = operationInvoker.invoke(transformedArguments);
return (O) multi.onItem().transform((t) -> {
try {
Object resultFromTransform = fieldHelper.transformOrAdaptResponse(t, dfe);
resultBuilder.data(resultFromTransform);
return (O) resultBuilder.build();
} catch (AbstractDataFetcherException abstractDataFetcherException) {
// Arguments or result couldn't be transformed
abstractDataFetcherException.appendDataFetcherResult(resultBuilder, dfe);
eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), abstractDataFetcherException);
return (O) resultBuilder.build();
}
}).onFailure().recoverWithItem(new Function<Throwable, O>() {
public O apply(Throwable throwable) {
eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), throwable);
if (throwable instanceof GraphQLException) {
GraphQLException graphQLException = (GraphQLException) throwable;
errorResultHelper.appendPartialResult(resultBuilder, dfe, graphQLException);
} else if (throwable instanceof Exception) {
DataFetcherException dataFetcherException = SmallRyeGraphQLServerMessages.msg.dataFetcherException(operation, throwable);
errorResultHelper.appendException(resultBuilder, dfe, dataFetcherException);
} else if (throwable instanceof Error) {
errorResultHelper.appendException(resultBuilder, dfe, throwable);
}
return (O) resultBuilder.build();
}
});
} finally {
SmallRyeContext.remove();
}
}
use of org.eclipse.microprofile.graphql.GraphQLException in project smallrye-graphql by smallrye.
the class PublisherDataFetcher method invokeAndTransform.
@Override
@SuppressWarnings("unchecked")
protected <O> O invokeAndTransform(DataFetchingEnvironment dfe, DataFetcherResult.Builder<Object> resultBuilder, Object[] transformedArguments) throws Exception {
SmallRyeContext context = contextHelper.getSmallRyeContext(dfe);
try {
SmallRyeContext.setContext(context);
Publisher<?> publisher = operationInvoker.invoke(transformedArguments);
Multi<?> multi = Multi.createFrom().publisher(publisher);
return (O) multi.onItem().transform((t) -> {
try {
Object resultFromTransform = fieldHelper.transformOrAdaptResponse(t, dfe);
resultBuilder.data(resultFromTransform);
return (O) resultBuilder.build();
} catch (AbstractDataFetcherException abstractDataFetcherException) {
// Arguments or result couldn't be transformed
abstractDataFetcherException.appendDataFetcherResult(resultBuilder, dfe);
eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), abstractDataFetcherException);
return (O) resultBuilder.build();
}
}).onFailure().recoverWithItem(new Function<Throwable, O>() {
@Override
public O apply(Throwable throwable) {
eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), throwable);
if (throwable instanceof GraphQLException) {
GraphQLException graphQLException = (GraphQLException) throwable;
errorResultHelper.appendPartialResult(resultBuilder, dfe, graphQLException);
} else if (throwable instanceof Exception) {
DataFetcherException dataFetcherException = SmallRyeGraphQLServerMessages.msg.dataFetcherException(operation, throwable);
errorResultHelper.appendException(resultBuilder, dfe, dataFetcherException);
} else if (throwable instanceof Error) {
errorResultHelper.appendException(resultBuilder, dfe, throwable);
}
return (O) resultBuilder.build();
}
});
} finally {
SmallRyeContext.remove();
}
}
Aggregations