Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Only call GraphqlErrorBuilder with non-null data #568

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,13 @@ private Collection<GraphQLError> withThrowable(Throwable throwable, ErrorContext
Map<String, Object> extensions = Optional.ofNullable(errorContext.getExtensions())
.orElseGet(HashMap::new);
extensions.put("type", throwable.getClass().getSimpleName());
return singletonList(
GraphqlErrorBuilder.newError()
.message(throwable.getMessage())
.errorType(errorContext.getErrorType())
.locations(errorContext.getLocations())
.path(errorContext.getPath())
.extensions(extensions)
.build()
);
GraphqlErrorBuilder builder = GraphqlErrorBuilder.newError()
.extensions(extensions);
Optional.ofNullable(throwable.getMessage()).ifPresent(builder::message);
Optional.ofNullable(errorContext.getErrorType()).ifPresent(builder::errorType);
Optional.ofNullable(errorContext.getLocations()).ifPresent(builder::locations);
Optional.ofNullable(errorContext.getPath()).ifPresent(builder::path);
return singletonList(builder.build());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package graphql.kickstart.spring.error;

import graphql.ErrorType;
import graphql.GraphQLError;
import graphql.GraphqlErrorException;
import graphql.language.SourceLocation;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class GraphQLErrorFromExceptionHandlerTest {
@Test
void allows_errors_with_null_path() {
GraphQLErrorFromExceptionHandler sut = new GraphQLErrorFromExceptionHandler(new ArrayList<>());

List<GraphQLError> errors = new ArrayList<>();
errors.add(GraphqlErrorException.newErrorException()
.message("Error without a path")
.sourceLocation(new SourceLocation(0, 0))
.build());
errors.add(GraphqlErrorException.newErrorException()
.message("Error with path")
.sourceLocation(new SourceLocation(0, 0))
.errorClassification(ErrorType.ValidationError)
.path(new ArrayList<>())
.build());

List<GraphQLError> processedErrors = sut.filterGraphQLErrors(errors);

for (int i = 0; i < errors.size(); i++) {
GraphQLError error = errors.get(i);
GraphQLError processedError = processedErrors.get(i);
assertEquals(error.getMessage(), processedError.getMessage());
assertEquals(error.getErrorType(), processedError.getErrorType());
assertEquals(error.getLocations(), processedError.getLocations());
assertEquals(error.getPath(), processedError.getPath());
}
}
}