Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Introduce a GraphqlErrorException for Kotlin users and a as a general…
… purpose exception that can be an error
  • Loading branch information
bbakerman committed Jan 15, 2020
commit ae9f6e0f14a8a0f8f4ba6e68ed396d0b075b565f
110 changes: 110 additions & 0 deletions src/main/java/graphql/GraphqlErrorException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package graphql;

import graphql.language.SourceLocation;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* A base class for graphql runtime exceptions that also implement {@link graphql.GraphQLError} and can be used
* in a general sense direct or have specialisations made of it.
* <p>
* This is aimed amongst other reasons at Kotlin consumers due to https://github.com/graphql-java/graphql-java/issues/1690
* as well as being a way to share common code.
*/
@PublicApi
public class GraphqlErrorException extends GraphQLException implements GraphQLError {

protected final List<SourceLocation> locations;
protected final Map<String, Object> extensions;
protected final List<Object> path;
protected final ErrorClassification errorClassification;

protected GraphqlErrorException(BuilderBase<?> builder) {
super(builder.message, builder.cause);
this.locations = builder.sourceLocations;
this.extensions = builder.extensions;
this.path = builder.path;
this.errorClassification = builder.errorClassification;
}

@Override
public List<SourceLocation> getLocations() {
return locations;
}

@Override
public ErrorClassification getErrorType() {
return errorClassification;
}

@Override
public List<Object> getPath() {
return path;
}

@Override
public Map<String, Object> getExtensions() {
return extensions;
}

public static Builder newErrorException() {
return new Builder();
}

public static class Builder extends BuilderBase<Builder> {
public GraphqlErrorException build() {
return new GraphqlErrorException(this);
}
}

protected static class BuilderBase<T extends BuilderBase<T>> {
protected String message;
protected Throwable cause;
protected ErrorClassification errorClassification = ErrorType.DataFetchingException;
protected List<SourceLocation> sourceLocations;
protected Map<String, Object> extensions;
protected List<Object> path;

private T asDerivedType() {
//noinspection unchecked
return (T) this;
}

public T message(String message) {
this.message = message;
return asDerivedType();
}

public T cause(Throwable cause) {
this.cause = cause;
return asDerivedType();
}

public T sourceLocation(SourceLocation sourceLocation) {
return sourceLocations(sourceLocation == null ? null : Collections.singletonList(sourceLocation));
}

public T sourceLocations(List<SourceLocation> sourceLocations) {
this.sourceLocations = sourceLocations;
return asDerivedType();
}

public T errorClassification(ErrorClassification errorClassification) {
this.errorClassification = errorClassification;
return asDerivedType();
}

public T path(List<Object> path) {
this.path = path;
return asDerivedType();
}

public T extensions(Map<String, Object> extensions) {
this.extensions = extensions;
return asDerivedType();
}

}
}
52 changes: 4 additions & 48 deletions src/main/java/graphql/schema/CoercingParseLiteralException.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package graphql.schema;

import graphql.ErrorType;
import graphql.GraphQLError;
import graphql.GraphQLException;
import graphql.GraphqlErrorException;
import graphql.PublicApi;
import graphql.language.SourceLocation;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@PublicApi
public class CoercingParseLiteralException extends GraphQLException implements GraphQLError {
private final List<SourceLocation> sourceLocations;
private final Map<String, Object> extensions;
public class CoercingParseLiteralException extends GraphqlErrorException {

public CoercingParseLiteralException() {
this(newCoercingParseLiteralException());
Expand All @@ -36,56 +29,19 @@ public CoercingParseLiteralException(Throwable cause) {
}

private CoercingParseLiteralException(Builder builder) {
super(builder.message, builder.cause);
this.sourceLocations = builder.sourceLocations;
this.extensions = builder.extensions;
}

@Override
public List<SourceLocation> getLocations() {
return sourceLocations;
super(builder);
}

@Override
public ErrorType getErrorType() {
return ErrorType.ValidationError;
}

@Override
public Map<String, Object> getExtensions() {
return extensions;
}

public static Builder newCoercingParseLiteralException() {
return new Builder();
}

public static class Builder {
private String message;
private Throwable cause;
private List<SourceLocation> sourceLocations;
private Map<String, Object> extensions;

public Builder message(String message) {
this.message = message;
return this;
}

public Builder cause(Throwable cause) {
this.cause = cause;
return this;
}

public Builder sourceLocation(SourceLocation sourceLocation) {
this.sourceLocations = sourceLocation == null ? null : Collections.singletonList(sourceLocation);
return this;
}

public Builder extensions(Map<String, Object> extensions) {
this.extensions = extensions;
return this;
}

public static class Builder extends BuilderBase<Builder> {
public CoercingParseLiteralException build() {
return new CoercingParseLiteralException(this);
}
Expand Down
57 changes: 4 additions & 53 deletions src/main/java/graphql/schema/CoercingParseValueException.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package graphql.schema;

import graphql.ErrorType;
import graphql.GraphQLError;
import graphql.GraphQLException;
import graphql.GraphqlErrorException;
import graphql.PublicApi;
import graphql.language.SourceLocation;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@PublicApi
public class CoercingParseValueException extends GraphQLException implements GraphQLError {
private final List<SourceLocation> sourceLocations;
private final Map<String, Object> extensions;
public class CoercingParseValueException extends GraphqlErrorException {

public CoercingParseValueException() {
this(newCoercingParseValueException());
Expand All @@ -36,61 +29,19 @@ public CoercingParseValueException(String message, Throwable cause, SourceLocati
}

private CoercingParseValueException(Builder builder) {
super(builder.message, builder.cause);
this.sourceLocations = builder.sourceLocations;
this.extensions = builder.extensions;
}

@Override
public List<SourceLocation> getLocations() {
return sourceLocations;
super(builder);
}

@Override
public ErrorType getErrorType() {
return ErrorType.ValidationError;
}

@Override
public Map<String, Object> getExtensions() {
return extensions;
}

public static Builder newCoercingParseValueException() {
return new Builder();
}

public static class Builder {
private String message;
private Throwable cause;
private List<SourceLocation> sourceLocations;
private Map<String, Object> extensions;

public Builder message(String message) {
this.message = message;
return this;
}

public Builder cause(Throwable cause) {
this.cause = cause;
return this;
}

public Builder sourceLocation(SourceLocation sourceLocation) {
this.sourceLocations = sourceLocation == null ? null : Collections.singletonList(sourceLocation);
return this;
}

public Builder sourceLocations(List<SourceLocation> sourceLocations) {
this.sourceLocations = sourceLocations;
return this;
}

public Builder extensions(Map<String, Object> extensions) {
this.extensions = extensions;
return this;
}

public static class Builder extends BuilderBase<Builder> {
public CoercingParseValueException build() {
return new CoercingParseValueException(this);
}
Expand Down
53 changes: 4 additions & 49 deletions src/main/java/graphql/schema/CoercingSerializeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@

import graphql.ErrorClassification;
import graphql.ErrorType;
import graphql.GraphQLError;
import graphql.GraphQLException;
import graphql.GraphqlErrorException;
import graphql.PublicApi;
import graphql.language.SourceLocation;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@PublicApi
public class CoercingSerializeException extends GraphQLException implements GraphQLError {
private final List<SourceLocation> sourceLocations;
private final Map<String, Object> extensions;
public class CoercingSerializeException extends GraphqlErrorException {

public CoercingSerializeException() {
this(newCoercingSerializeException());
Expand All @@ -33,56 +25,19 @@ public CoercingSerializeException(Throwable cause) {
}

private CoercingSerializeException(Builder builder) {
super(builder.message, builder.cause);
this.sourceLocations = builder.sourceLocations;
this.extensions = builder.extensions;
}

@Override
public List<SourceLocation> getLocations() {
return sourceLocations;
super(builder);
}

@Override
public ErrorClassification getErrorType() {
return ErrorType.DataFetchingException;
}

@Override
public Map<String, Object> getExtensions() {
return extensions;
}

public static Builder newCoercingSerializeException() {
return new Builder();
}

public static class Builder {
private String message;
private Throwable cause;
private Map<String, Object> extensions;
private List<SourceLocation> sourceLocations;

public Builder message(String message) {
this.message = message;
return this;
}

public Builder cause(Throwable cause) {
this.cause = cause;
return this;
}

public Builder sourceLocation(SourceLocation sourceLocation) {
this.sourceLocations = sourceLocation == null ? null : Collections.singletonList(sourceLocation);
return this;
}

public Builder extensions(Map<String, Object> extensions) {
this.extensions = extensions;
return this;
}

public static class Builder extends BuilderBase<Builder> {
public CoercingSerializeException build() {
return new CoercingSerializeException(this);
}
Expand Down