Skip to content

Scalars: convert errors from 'TypeError' to 'GraphQLError' #2187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2019
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
4 changes: 2 additions & 2 deletions src/execution/__tests__/variables-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Variable "$value" got invalid value [1, 2, 3]; Expected type "String". String cannot represent a non string value: [1, 2, 3]',
'Variable "$value" got invalid value [1, 2, 3]; String cannot represent a non string value: [1, 2, 3]',
locations: [{ line: 2, column: 16 }],
},
],
Expand Down Expand Up @@ -1006,7 +1006,7 @@ describe('Execute: Handles inputs', () => {

function invalidValueError(value, index) {
return {
message: `Variable "$input" got invalid value ${value} at "input[${index}]"; Expected type "String". String cannot represent a non string value: ${value}`,
message: `Variable "$input" got invalid value ${value} at "input[${index}]"; String cannot represent a non string value: ${value}`,
locations: [{ line: 2, column: 14 }],
};
}
Expand Down
5 changes: 1 addition & 4 deletions src/subscription/__tests__/subscribe-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,6 @@ describe('Subscription Initialization Phase', () => {
},
};

// $FlowFixMe
const result = await subscribe(emailSchema, ast, data, null, {
priority: 'meow',
});
Expand All @@ -528,13 +527,11 @@ describe('Subscription Initialization Phase', () => {
errors: [
{
message:
'Variable "$priority" got invalid value "meow"; Expected type "Int". Int cannot represent non-integer value: "meow"',
'Variable "$priority" got invalid value "meow"; Int cannot represent non-integer value: "meow"',
locations: [{ line: 2, column: 21 }],
},
],
});

expect(result.errors[0].originalError).not.to.equal(undefined);
});
});

Expand Down
3 changes: 1 addition & 2 deletions src/type/__tests__/enumType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ describe('Type System: Enum Values', () => {
expect(result).to.deep.equal({
errors: [
{
message:
'Expected value of type "Int", found GREEN; Int cannot represent non-integer value: GREEN',
message: 'Int cannot represent non-integer value: GREEN',
locations: [{ line: 1, column: 22 }],
},
],
Expand Down
44 changes: 26 additions & 18 deletions src/type/scalars.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import isObjectLike from '../jsutils/isObjectLike';
import { Kind } from '../language/kinds';
import { print } from '../language/printer';

import { GraphQLError } from '../error/GraphQLError';

import { GraphQLScalarType, isScalarType } from './definition';

// As per the GraphQL Spec, Integers are only treated as valid when a valid
Expand All @@ -30,12 +32,12 @@ function serializeInt(value: mixed): number {
}

if (!isInteger(num)) {
throw new TypeError(
throw new GraphQLError(
`Int cannot represent non-integer value: ${inspect(value)}`,
);
}
if (num > MAX_INT || num < MIN_INT) {
throw new TypeError(
throw new GraphQLError(
`Int cannot represent non 32-bit signed integer value: ${inspect(value)}`,
);
}
Expand All @@ -44,12 +46,12 @@ function serializeInt(value: mixed): number {

function coerceInt(value: mixed): number {
if (!isInteger(value)) {
throw new TypeError(
throw new GraphQLError(
`Int cannot represent non-integer value: ${inspect(value)}`,
);
}
if (value > MAX_INT || value < MIN_INT) {
throw new TypeError(
throw new GraphQLError(
`Int cannot represent non 32-bit signed integer value: ${inspect(value)}`,
);
}
Expand All @@ -64,14 +66,16 @@ export const GraphQLInt = new GraphQLScalarType({
parseValue: coerceInt,
parseLiteral(ast) {
if (ast.kind !== Kind.INT) {
throw new TypeError(
throw new GraphQLError(
'Int cannot represent non-integer value: ' + print(ast),
ast,
);
}
const num = parseInt(ast.value, 10);
if (num > MAX_INT || num < MIN_INT) {
throw new TypeError(
throw new GraphQLError(
'Int cannot represent non 32-bit signed integer value: ' + ast.value,
ast,
);
}
return num;
Expand All @@ -88,7 +92,7 @@ function serializeFloat(value: mixed): number {
num = Number(value);
}
if (!isFinite(num)) {
throw new TypeError(
throw new GraphQLError(
`Float cannot represent non numeric value: ${inspect(value)}`,
);
}
Expand All @@ -97,7 +101,7 @@ function serializeFloat(value: mixed): number {

function coerceFloat(value: mixed): number {
if (!isFinite(value)) {
throw new TypeError(
throw new GraphQLError(
`Float cannot represent non numeric value: ${inspect(value)}`,
);
}
Expand All @@ -112,8 +116,9 @@ export const GraphQLFloat = new GraphQLScalarType({
parseValue: coerceFloat,
parseLiteral(ast) {
if (ast.kind !== Kind.FLOAT && ast.kind !== Kind.INT) {
throw new TypeError(
throw new GraphQLError(
'Float cannot represent non numeric value: ' + print(ast),
ast,
);
}
return parseFloat(ast.value);
Expand Down Expand Up @@ -153,12 +158,12 @@ function serializeString(rawValue: mixed): string {
if (isFinite(value)) {
return value.toString();
}
throw new TypeError(`String cannot represent value: ${inspect(rawValue)}`);
throw new GraphQLError(`String cannot represent value: ${inspect(rawValue)}`);
}

function coerceString(value: mixed): string {
if (typeof value !== 'string') {
throw new TypeError(
throw new GraphQLError(
`String cannot represent a non string value: ${inspect(value)}`,
);
}
Expand All @@ -173,8 +178,9 @@ export const GraphQLString = new GraphQLScalarType({
parseValue: coerceString,
parseLiteral(ast) {
if (ast.kind !== Kind.STRING) {
throw new TypeError(
throw new GraphQLError(
'String cannot represent a non string value: ' + print(ast),
ast,
);
}
return ast.value;
Expand All @@ -188,14 +194,14 @@ function serializeBoolean(value: mixed): boolean {
if (isFinite(value)) {
return value !== 0;
}
throw new TypeError(
throw new GraphQLError(
`Boolean cannot represent a non boolean value: ${inspect(value)}`,
);
}

function coerceBoolean(value: mixed): boolean {
if (typeof value !== 'boolean') {
throw new TypeError(
throw new GraphQLError(
`Boolean cannot represent a non boolean value: ${inspect(value)}`,
);
}
Expand All @@ -209,8 +215,9 @@ export const GraphQLBoolean = new GraphQLScalarType({
parseValue: coerceBoolean,
parseLiteral(ast) {
if (ast.kind !== Kind.BOOLEAN) {
throw new TypeError(
throw new GraphQLError(
'Boolean cannot represent a non boolean value: ' + print(ast),
ast,
);
}
return ast.value;
Expand All @@ -226,7 +233,7 @@ function serializeID(rawValue: mixed): string {
if (isInteger(value)) {
return String(value);
}
throw new TypeError(`ID cannot represent value: ${inspect(rawValue)}`);
throw new GraphQLError(`ID cannot represent value: ${inspect(rawValue)}`);
}

function coerceID(value: mixed): string {
Expand All @@ -236,7 +243,7 @@ function coerceID(value: mixed): string {
if (isInteger(value)) {
return value.toString();
}
throw new TypeError(`ID cannot represent value: ${inspect(value)}`);
throw new GraphQLError(`ID cannot represent value: ${inspect(value)}`);
}

export const GraphQLID = new GraphQLScalarType({
Expand All @@ -247,8 +254,9 @@ export const GraphQLID = new GraphQLScalarType({
parseValue: coerceID,
parseLiteral(ast) {
if (ast.kind !== Kind.STRING && ast.kind !== Kind.INT) {
throw new TypeError(
throw new GraphQLError(
'ID cannot represent a non-string and non-integer value: ' + print(ast),
ast,
);
}
return ast.value;
Expand Down
18 changes: 6 additions & 12 deletions src/utilities/__tests__/coerceInputValue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ describe('coerceInputValue', () => {
const result = coerceValue({ foo: NaN }, TestInputObject);
expectErrors(result).to.deep.equal([
{
error:
'Expected type "Int". Int cannot represent non-integer value: NaN',
error: 'Int cannot represent non-integer value: NaN',
path: ['foo'],
value: NaN,
},
Expand All @@ -209,14 +208,12 @@ describe('coerceInputValue', () => {
const result = coerceValue({ foo: 'abc', bar: 'def' }, TestInputObject);
expectErrors(result).to.deep.equal([
{
error:
'Expected type "Int". Int cannot represent non-integer value: "abc"',
error: 'Int cannot represent non-integer value: "abc"',
path: ['foo'],
value: 'abc',
},
{
error:
'Expected type "Int". Int cannot represent non-integer value: "def"',
error: 'Int cannot represent non-integer value: "def"',
path: ['bar'],
value: 'def',
},
Expand Down Expand Up @@ -309,14 +306,12 @@ describe('coerceInputValue', () => {
const result = coerceValue([1, 'b', true, 4], TestList);
expectErrors(result).to.deep.equal([
{
error:
'Expected type "Int". Int cannot represent non-integer value: "b"',
error: 'Int cannot represent non-integer value: "b"',
path: [1],
value: 'b',
},
{
error:
'Expected type "Int". Int cannot represent non-integer value: true',
error: 'Int cannot represent non-integer value: true',
path: [2],
value: true,
},
Expand All @@ -332,8 +327,7 @@ describe('coerceInputValue', () => {
const result = coerceValue('INVALID', TestList);
expectErrors(result).to.deep.equal([
{
error:
'Expected type "Int". Int cannot represent non-integer value: "INVALID"',
error: 'Int cannot represent non-integer value: "INVALID"',
path: [],
value: 'INVALID',
},
Expand Down
28 changes: 16 additions & 12 deletions src/utilities/coerceInputValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,22 @@ function coerceInputValueImpl(
try {
parseResult = type.parseValue(inputValue);
} catch (error) {
onError(
pathToArray(path),
inputValue,
new GraphQLError(
`Expected type "${type.name}". ` + error.message,
undefined,
undefined,
undefined,
undefined,
error,
),
);
if (error instanceof GraphQLError) {
onError(pathToArray(path), inputValue, error);
} else {
onError(
pathToArray(path),
inputValue,
new GraphQLError(
`Expected type "${type.name}". ` + error.message,
undefined,
undefined,
undefined,
undefined,
error,
),
);
}
return;
}
if (parseResult === undefined) {
Expand Down
Loading