apollo-server-errors
Advanced tools
Comparing version 3.0.0-preview.0 to 3.0.0-preview.1
@@ -31,6 +31,6 @@ import { ASTNode, GraphQLError, GraphQLFormattedError, Source, SourceLocation } from 'graphql'; | ||
export declare class AuthenticationError extends ApolloError { | ||
constructor(message: string); | ||
constructor(message: string, extensions?: Record<string, any>); | ||
} | ||
export declare class ForbiddenError extends ApolloError { | ||
constructor(message: string); | ||
constructor(message: string, extensions?: Record<string, any>); | ||
} | ||
@@ -44,3 +44,3 @@ export declare class PersistedQueryNotFoundError extends ApolloError { | ||
export declare class UserInputError extends ApolloError { | ||
constructor(message: string, properties?: Record<string, any>); | ||
constructor(message: string, extensions?: Record<string, any>); | ||
} | ||
@@ -51,3 +51,2 @@ export declare function formatApolloErrors(errors: ReadonlyArray<Error>, options?: { | ||
}): Array<ApolloError>; | ||
export declare function hasPersistedQueryError(errors: Array<Error>): boolean; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.hasPersistedQueryError = exports.formatApolloErrors = exports.UserInputError = exports.PersistedQueryNotSupportedError = exports.PersistedQueryNotFoundError = exports.ForbiddenError = exports.AuthenticationError = exports.ValidationError = exports.SyntaxError = exports.fromGraphQLError = exports.toApolloError = exports.ApolloError = void 0; | ||
exports.formatApolloErrors = exports.UserInputError = exports.PersistedQueryNotSupportedError = exports.PersistedQueryNotFoundError = exports.ForbiddenError = exports.AuthenticationError = exports.ValidationError = exports.SyntaxError = exports.fromGraphQLError = exports.toApolloError = exports.ApolloError = void 0; | ||
const graphql_1 = require("graphql"); | ||
@@ -8,14 +8,11 @@ class ApolloError extends Error { | ||
super(message); | ||
if (extensions) { | ||
Object.keys(extensions) | ||
.filter(keyName => keyName !== 'message' && keyName !== 'extensions') | ||
.forEach(key => { | ||
this[key] = extensions[key]; | ||
}); | ||
} | ||
if (!this.name) { | ||
Object.defineProperty(this, 'name', { value: 'ApolloError' }); | ||
} | ||
const userProvidedExtensions = (extensions && extensions.extensions) || null; | ||
this.extensions = { ...extensions, ...userProvidedExtensions, code }; | ||
if (extensions === null || extensions === void 0 ? void 0 : extensions.extensions) { | ||
throw Error('Pass extensions directly as the third argument of the ApolloError constructor: `new ' + | ||
'ApolloError(message, code, {myExt: value})`, not `new ApolloError(message, code, ' + | ||
'{extensions: {myExt: value}})`'); | ||
} | ||
this.extensions = { ...extensions, code }; | ||
} | ||
@@ -102,4 +99,6 @@ } | ||
Object.defineProperty(copy, 'originalError', { value: {} }); | ||
Object.getOwnPropertyNames(error).forEach(key => { | ||
Object.defineProperty(copy.originalError, key, { value: error[key] }); | ||
Object.getOwnPropertyNames(error).forEach((key) => { | ||
Object.defineProperty(copy.originalError, key, { | ||
value: error[key], | ||
}); | ||
}); | ||
@@ -124,4 +123,4 @@ return copy; | ||
class AuthenticationError extends ApolloError { | ||
constructor(message) { | ||
super(message, 'UNAUTHENTICATED'); | ||
constructor(message, extensions) { | ||
super(message, 'UNAUTHENTICATED', extensions); | ||
Object.defineProperty(this, 'name', { value: 'AuthenticationError' }); | ||
@@ -132,4 +131,4 @@ } | ||
class ForbiddenError extends ApolloError { | ||
constructor(message) { | ||
super(message, 'FORBIDDEN'); | ||
constructor(message, extensions) { | ||
super(message, 'FORBIDDEN', extensions); | ||
Object.defineProperty(this, 'name', { value: 'ForbiddenError' }); | ||
@@ -158,4 +157,4 @@ } | ||
class UserInputError extends ApolloError { | ||
constructor(message, properties) { | ||
super(message, 'BAD_USER_INPUT', properties); | ||
constructor(message, extensions) { | ||
super(message, 'BAD_USER_INPUT', extensions); | ||
Object.defineProperty(this, 'name', { value: 'UserInputError' }); | ||
@@ -167,6 +166,6 @@ } | ||
if (!options) { | ||
return errors.map(error => enrichError(error)); | ||
return errors.map((error) => enrichError(error)); | ||
} | ||
const { formatter, debug } = options; | ||
const enrichedErrors = errors.map(error => enrichError(error, debug)); | ||
const enrichedErrors = errors.map((error) => enrichError(error, debug)); | ||
const makePrintable = (error) => { | ||
@@ -187,3 +186,3 @@ if (error instanceof Error) { | ||
} | ||
return enrichedErrors.map(error => { | ||
return enrichedErrors.map((error) => { | ||
try { | ||
@@ -204,9 +203,2 @@ return makePrintable(formatter(error)); | ||
exports.formatApolloErrors = formatApolloErrors; | ||
function hasPersistedQueryError(errors) { | ||
return Array.isArray(errors) | ||
? errors.some(error => error instanceof PersistedQueryNotFoundError || | ||
error instanceof PersistedQueryNotSupportedError) | ||
: false; | ||
} | ||
exports.hasPersistedQueryError = hasPersistedQueryError; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "apollo-server-errors", | ||
"version": "3.0.0-preview.0", | ||
"version": "3.0.0-preview.1", | ||
"author": "Apollo <opensource@apollographql.com>", | ||
@@ -23,3 +23,3 @@ "license": "MIT", | ||
}, | ||
"gitHead": "68b776f0a08d7361efecf40e0a9e344b19b3e4b8" | ||
"gitHead": "f23a4eb1aa5c5ae8cf83a1f41bd9bae3a03d3360" | ||
} |
@@ -1,2 +0,2 @@ | ||
import { ApolloError } from '..'; | ||
import { ApolloError, ForbiddenError, AuthenticationError } from '..'; | ||
@@ -40,11 +40,38 @@ describe('ApolloError', () => { | ||
it('(back-compat) sets extensions correctly for users who use an extensions key in the third constructor argument', () => { | ||
const error = new ApolloError('My original message', 'A_CODE', { | ||
extensions: { | ||
arbitrary: 'user_data', | ||
}, | ||
it('throws for users who use an extensions key in the third constructor argument', () => { | ||
expect( | ||
() => | ||
new ApolloError('My original message', 'A_CODE', { | ||
extensions: { | ||
arbitrary: 'user_data', | ||
}, | ||
}), | ||
).toThrow(/Pass extensions directly/); | ||
}); | ||
}); | ||
describe("ForbiddenError", () => { | ||
it("supports abritrary data being passed", () => { | ||
const error = new ForbiddenError('My message', { | ||
arbitrary: 'user_data', | ||
}); | ||
expect(error.extensions.arbitrary).toEqual('user_data'); | ||
}); | ||
}); | ||
expect(error.extensions).toEqual({ | ||
code: 'FORBIDDEN', | ||
arbitrary: 'user_data', | ||
}); | ||
}) | ||
}) | ||
describe("AuthenticationError", () => { | ||
it("supports abritrary data being passed", () => { | ||
const error = new AuthenticationError('My message', { | ||
arbitrary: 'user_data', | ||
}); | ||
expect(error.extensions).toEqual({ | ||
code: 'UNAUTHENTICATED', | ||
arbitrary: 'user_data', | ||
}); | ||
}) | ||
}) |
@@ -1,2 +0,8 @@ | ||
import { ASTNode, GraphQLError, GraphQLFormattedError, Source, SourceLocation } from 'graphql'; | ||
import { | ||
ASTNode, | ||
GraphQLError, | ||
GraphQLFormattedError, | ||
Source, | ||
SourceLocation, | ||
} from 'graphql'; | ||
@@ -22,24 +28,2 @@ export class ApolloError extends Error implements GraphQLError { | ||
// This variable was previously named `properties`, which allowed users to set | ||
// arbitrary properties on the ApolloError object. This use case is still supported, | ||
// but deprecated in favor of using the ApolloError.extensions object instead. | ||
// This change intends to comply with the GraphQL spec on errors. See: | ||
// https://github.com/graphql/graphql-spec/blob/master/spec/Section%207%20--%20Response.md#response-format | ||
// | ||
// Going forward, users should use the ApolloError.extensions object for storing | ||
// and reading arbitrary data on an error, as arbitrary properties on the ApolloError | ||
// itself won't be supported in the future. | ||
// | ||
// XXX Filter 'message' and 'extensions' specifically so they don't overwrite the class property. | ||
// We _could_ filter all of the class properties, but have chosen to only do | ||
// so if it's an issue for other users. Please feel free to open an issue if you | ||
// find yourself here with this exact problem. | ||
if (extensions) { | ||
Object.keys(extensions) | ||
.filter(keyName => keyName !== 'message' && keyName !== 'extensions') | ||
.forEach(key => { | ||
this[key] = extensions[key]; | ||
}); | ||
} | ||
// if no name provided, use the default. defineProperty ensures that it stays non-enumerable | ||
@@ -50,8 +34,11 @@ if (!this.name) { | ||
// Before the mentioned change to extensions, users could previously set the extensions | ||
// object by providing it as a key on the third argument to the constructor. | ||
// This step provides backwards compatibility for those hypothetical users. | ||
const userProvidedExtensions = (extensions && extensions.extensions) || null; | ||
if (extensions?.extensions) { | ||
throw Error( | ||
'Pass extensions directly as the third argument of the ApolloError constructor: `new ' + | ||
'ApolloError(message, code, {myExt: value})`, not `new ApolloError(message, code, ' + | ||
'{extensions: {myExt: value}})`', | ||
); | ||
} | ||
this.extensions = { ...extensions, ...userProvidedExtensions, code }; | ||
this.extensions = { ...extensions, code }; | ||
} | ||
@@ -169,4 +156,6 @@ } | ||
Object.defineProperty(copy, 'originalError', { value: {} }); | ||
Object.getOwnPropertyNames(error).forEach(key => { | ||
Object.defineProperty(copy.originalError, key, { value: (error as any)[key] }); | ||
Object.getOwnPropertyNames(error).forEach((key) => { | ||
Object.defineProperty(copy.originalError, key, { | ||
value: (error as any)[key], | ||
}); | ||
}); | ||
@@ -194,4 +183,4 @@ | ||
export class AuthenticationError extends ApolloError { | ||
constructor(message: string) { | ||
super(message, 'UNAUTHENTICATED'); | ||
constructor(message: string, extensions?: Record<string, any>) { | ||
super(message, 'UNAUTHENTICATED', extensions); | ||
@@ -203,4 +192,4 @@ Object.defineProperty(this, 'name', { value: 'AuthenticationError' }); | ||
export class ForbiddenError extends ApolloError { | ||
constructor(message: string) { | ||
super(message, 'FORBIDDEN'); | ||
constructor(message: string, extensions?: Record<string, any>) { | ||
super(message, 'FORBIDDEN', extensions); | ||
@@ -232,4 +221,4 @@ Object.defineProperty(this, 'name', { value: 'ForbiddenError' }); | ||
export class UserInputError extends ApolloError { | ||
constructor(message: string, properties?: Record<string, any>) { | ||
super(message, 'BAD_USER_INPUT', properties); | ||
constructor(message: string, extensions?: Record<string, any>) { | ||
super(message, 'BAD_USER_INPUT', extensions); | ||
@@ -248,3 +237,3 @@ Object.defineProperty(this, 'name', { value: 'UserInputError' }); | ||
if (!options) { | ||
return errors.map(error => enrichError(error)); | ||
return errors.map((error) => enrichError(error)); | ||
} | ||
@@ -273,3 +262,3 @@ const { formatter, debug } = options; | ||
const enrichedErrors = errors.map(error => enrichError(error, debug)); | ||
const enrichedErrors = errors.map((error) => enrichError(error, debug)); | ||
const makePrintable = (error: GraphQLFormattedError) => { | ||
@@ -293,3 +282,3 @@ if (error instanceof Error) { | ||
return enrichedErrors.map(error => { | ||
return enrichedErrors.map((error) => { | ||
try { | ||
@@ -310,11 +299,1 @@ return makePrintable(formatter(error)); | ||
} | ||
export function hasPersistedQueryError(errors: Array<Error>): boolean { | ||
return Array.isArray(errors) | ||
? errors.some( | ||
error => | ||
error instanceof PersistedQueryNotFoundError || | ||
error instanceof PersistedQueryNotSupportedError, | ||
) | ||
: false; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60606
567