Skip to content

Commit

Permalink
feat : add exceptionResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Mar 10, 2024
1 parent 5fd5eda commit f1c9143
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ public class CustomExceptionResolver extends DataFetcherExceptionResolverAdapter

@Override
protected GraphQLError resolveToSingleError(Throwable ex, DataFetchingEnvironment env) {
ConstraintViolationException validationException = (ConstraintViolationException) ex;
return GraphqlErrorBuilder.newError()
.errorType(ErrorType.ValidationError)
.message(validationException.getMessage())
.path(env.getExecutionStepInfo().getPath())
.location(env.getField().getSourceLocation())
.build();

if (ex instanceof ConstraintViolationException) {
return GraphqlErrorBuilder.newError()
.errorType(ErrorType.ValidationError)
.message(ex.getMessage())
.path(env.getExecutionStepInfo().getPath())
.location(env.getField().getSourceLocation())
.build();
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.graphql.exception;

import graphql.ErrorType;
import graphql.GraphQLError;
import graphql.GraphqlErrorBuilder;
import graphql.schema.DataFetchingEnvironment;
import jakarta.validation.ConstraintViolationException;
import org.springframework.graphql.execution.DataFetcherExceptionResolverAdapter;
import org.springframework.stereotype.Component;

@Component
public class CustomExceptionResolver extends DataFetcherExceptionResolverAdapter {

@Override
protected GraphQLError resolveToSingleError(Throwable ex, DataFetchingEnvironment env) {

if (ex instanceof ConstraintViolationException) {
return GraphqlErrorBuilder.newError()
.errorType(ErrorType.ValidationError)
.message(ex.getMessage())
.path(env.getExecutionStepInfo().getPath())
.location(env.getField().getSourceLocation())
.build();
} else {
return null;
}
}
}

0 comments on commit f1c9143

Please sign in to comment.