Skip to content
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
6 changes: 6 additions & 0 deletions src/main/java/graphql/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public static <T> T assertNotNull(T object, String errorMessage) {
throw new AssertException(errorMessage);
}

public static <T> T assertNotNull(T object) {
if (object != null) return object;
throw new AssertException("Object required to be not null");
}

public static <T> T assertNeverCalled() {
throw new AssertException("Should never been called");
}
Expand Down Expand Up @@ -39,6 +44,7 @@ public static void assertTrue(boolean condition, String errorMessage) {
* currently non null, non empty,
*
* @param name - the name to be validated.
*
* @return the name if valid, or AssertException if invalid.
*/
public static String assertValidName(String name) {
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/graphql/ExceptionWhileDataFetching.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package graphql;


import graphql.execution.ExecutionPath;
import graphql.language.SourceLocation;

import java.util.List;

import static graphql.Assert.assertNotNull;
import static java.lang.String.format;

@PublicApi
public class ExceptionWhileDataFetching implements GraphQLError {

private final ExecutionPath path;
private final Throwable exception;

public ExceptionWhileDataFetching(Throwable exception) {
this.exception = exception;
public ExceptionWhileDataFetching(ExecutionPath path, Throwable exception) {
this.path = assertNotNull(path);
this.exception = assertNotNull(exception);
}

public Throwable getException() {
Expand All @@ -21,14 +27,24 @@ public Throwable getException() {

@Override
public String getMessage() {
return "Exception while fetching data: " + exception.getMessage();
return format("Exception while fetching data (%s) : %s", path, exception.getMessage());
}

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

/**
* The graphql spec says that that path field of any error should be a list
* of path entries - http://facebook.github.io/graphql/#sec-Errors
*
* @return the path in list format
*/
public List<Object> getPath() {
return path.toList();
}

@Override
public ErrorType getErrorType() {
return ErrorType.DataFetchingException;
Expand All @@ -37,10 +53,12 @@ public ErrorType getErrorType() {
@Override
public String toString() {
return "ExceptionWhileDataFetching{" +
"path=" + path +
"exception=" + exception +
'}';
}

@SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
@Override
public boolean equals(Object o) {
return Helper.equals(this, o);
Expand Down
48 changes: 21 additions & 27 deletions src/main/java/graphql/Scalars.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ public class Scalars {


private static boolean isNumberIsh(Object input) {
if (input instanceof Number) {
return true;
}
if (input instanceof String) {
return true;
}
return false;
return input instanceof Number || input instanceof String;
}

public static GraphQLScalarType GraphQLInt = new GraphQLScalarType("Int", "Built-in Int", new Coercing<Integer, Integer>() {
Expand Down Expand Up @@ -64,7 +58,7 @@ private Integer convertImpl(Object input) {
public Integer serialize(Object input) {
Integer result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException("Invalid value " + input + " for Int");
throw new CoercingSerializeException("Invalid value '" + input + "' for Int");
}
return result;
}
Expand All @@ -73,7 +67,7 @@ public Integer serialize(Object input) {
public Integer parseValue(Object input) {
Integer result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException("Invalid value " + input + " for Int");
throw new CoercingParseValueException("Invalid value '" + input + "' for Int");
}
return result;
}
Expand Down Expand Up @@ -115,7 +109,7 @@ private Long convertImpl(Object input) {
public Long serialize(Object input) {
Long result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException("Invalid input " + input + " for Long");
throw new CoercingSerializeException("Invalid input '" + input + "' for Long");
}
return result;
}
Expand All @@ -124,7 +118,7 @@ public Long serialize(Object input) {
public Long parseValue(Object input) {
Long result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException("Invalid input " + input + " for Long");
throw new CoercingParseValueException("Invalid input '" + input + "' for Long");
}
return result;
}
Expand Down Expand Up @@ -175,7 +169,7 @@ private Short convertImpl(Object input) {
public Short serialize(Object input) {
Short result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException("Invalid input " + input + " for Short");
throw new CoercingSerializeException("Invalid input '" + input + "' for Short");
}
return result;
}
Expand All @@ -184,7 +178,7 @@ public Short serialize(Object input) {
public Short parseValue(Object input) {
Short result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException("Invalid input " + input + " for Short");
throw new CoercingParseValueException("Invalid input '" + input + "' for Short");
}
return result;
}
Expand Down Expand Up @@ -226,7 +220,7 @@ private Byte convertImpl(Object input) {
public Byte serialize(Object input) {
Byte result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException("Invalid input " + input + " for Byte");
throw new CoercingSerializeException("Invalid input '" + input + "' for Byte");
}
return result;
}
Expand All @@ -235,7 +229,7 @@ public Byte serialize(Object input) {
public Byte parseValue(Object input) {
Byte result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException("Invalid input " + input + " for Byte");
throw new CoercingParseValueException("Invalid input '" + input + "' for Byte");
}
return result;
}
Expand Down Expand Up @@ -276,7 +270,7 @@ private Double convertImpl(Object input) {
public Double serialize(Object input) {
Double result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException("Invalid input " + input + " for Byte");
throw new CoercingSerializeException("Invalid input '" + input + "' for Float");
}
return result;

Expand All @@ -286,7 +280,7 @@ public Double serialize(Object input) {
public Double parseValue(Object input) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were some copy and pasta errors in the messages. I fixed them and put the offending object in 'quotes'

Double result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException("Invalid input " + input + " for Byte");
throw new CoercingParseValueException("Invalid input '" + input + "' for Float");
}
return result;
}
Expand Down Expand Up @@ -327,7 +321,7 @@ private BigInteger convertImpl(Object input) {
public BigInteger serialize(Object input) {
BigInteger result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException("Invalid input " + input + " for BigDecimal");
throw new CoercingSerializeException("Invalid input '" + input + "' for BigInteger");
}
return result;
}
Expand All @@ -336,7 +330,7 @@ public BigInteger serialize(Object input) {
public BigInteger parseValue(Object input) {
BigInteger result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException("Invalid input " + input + " for BigDecimal");
throw new CoercingParseValueException("Invalid input '" + input + "' for BigInteger");
}
return result;
}
Expand Down Expand Up @@ -380,7 +374,7 @@ private BigDecimal convertImpl(Object input) {
public BigDecimal serialize(Object input) {
BigDecimal result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException("Invalid input " + input + " for BigDecimal");
throw new CoercingSerializeException("Invalid input '" + input + "' for BigDecimal");
}
return result;
}
Expand All @@ -389,7 +383,7 @@ public BigDecimal serialize(Object input) {
public BigDecimal parseValue(Object input) {
BigDecimal result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException("Invalid input " + input + " for BigDecimal");
throw new CoercingParseValueException("Invalid input '" + input + "' for BigDecimal");
}
return result;
}
Expand Down Expand Up @@ -457,7 +451,7 @@ private Boolean convertImpl(Object input) {
public Boolean serialize(Object input) {
Boolean result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException("Invalid input " + input + " for Boolean");
throw new CoercingSerializeException("Invalid input '" + input + "' for Boolean");
}
return result;
}
Expand All @@ -466,7 +460,7 @@ public Boolean serialize(Object input) {
public Boolean parseValue(Object input) {
Boolean result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException("Invalid input " + input + " for Boolean");
throw new CoercingParseValueException("Invalid input '" + input + "' for Boolean");
}
return result;
}
Expand Down Expand Up @@ -498,7 +492,7 @@ private String convertImpl(Object input) {
public String serialize(Object input) {
String result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException("Invalid input " + input + " for ID");
throw new CoercingSerializeException("Invalid input '" + input + "' for ID");
}
return result;
}
Expand All @@ -507,7 +501,7 @@ public String serialize(Object input) {
public String parseValue(Object input) {
String result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException("Invalid input " + input + " for ID");
throw new CoercingParseValueException("Invalid input '" + input + "' for ID");
}
return result;
}
Expand Down Expand Up @@ -542,7 +536,7 @@ private Character convertImpl(Object input) {
public Character serialize(Object input) {
Character result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException("Invalid input " + input + " for Char");
throw new CoercingSerializeException("Invalid input '" + input + "' for Char");
}
return result;
}
Expand All @@ -551,7 +545,7 @@ public Character serialize(Object input) {
public Character parseValue(Object input) {
Character result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException("Invalid input " + input + " for Char");
throw new CoercingParseValueException("Invalid input '" + input + "' for Char");
}
return result;
}
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/graphql/SerializationError.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package graphql;


import graphql.execution.ExecutionPath;
import graphql.language.SourceLocation;
import graphql.schema.CoercingSerializeException;

import java.util.List;

import static graphql.Assert.assertNotNull;
import static java.lang.String.format;

@PublicApi
public class SerializationError implements GraphQLError {

private final CoercingSerializeException exception;
private final ExecutionPath path;

public SerializationError(CoercingSerializeException exception) {
this.exception = exception;
public SerializationError(ExecutionPath path, CoercingSerializeException exception) {
this.path = assertNotNull(path);
this.exception = assertNotNull(exception);
}

public CoercingSerializeException getException() {
Expand All @@ -22,7 +28,7 @@ public CoercingSerializeException getException() {

@Override
public String getMessage() {
return "Can't serialize value: " + exception.getMessage();
return format("Can't serialize value (%s) : %s", path, exception.getMessage());
}

@Override
Expand All @@ -35,13 +41,25 @@ public ErrorType getErrorType() {
return ErrorType.DataFetchingException;
}

/**
* The graphql spec says that that path field of any error should be a list
* of path entries - http://facebook.github.io/graphql/#sec-Errors
*
* @return the path in list format
*/
public List<Object> getPath() {
return path.toList();
}

@Override
public String toString() {
return "ExceptionWhileDataFetching{" +
"path=" + path +
"exception=" + exception +
'}';
}

@SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
@Override
public boolean equals(Object o) {
return Helper.equals(this, o);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/graphql/execution/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ private ExecutionResult executeOperation(
.source(root)
.fields(fields)
.nonNullFieldValidator(nonNullableFieldValidator)
.path(ExecutionPath.rootPath())
.build();

ExecutionResult result;
Expand Down
Loading