Skip to content

Class: ForbiddenError

Defined in: packages/apollo/src/errors/forbidden.error.ts:20

Error thrown when the user is not authorized to access a resource.

This class was removed in Apollo Server 4.0.0. This implementation provides the same functionality.

Example

typescript
@Query()
async adminData(@Context() ctx: MyContext) {
  if (!ctx.user.isAdmin) {
    throw new ForbiddenError('Admin access required');
  }
  return this.service.getAdminData();
}

Extends

  • GraphQLError

Accessors

[toStringTag]

Get Signature

ts
get toStringTag: string;

Defined in: node_modules/graphql/error/GraphQLError.d.ts:192

Returns the value used by Object.prototype.toString.

Returns

string

The built-in string tag for this object.

Inherited from

ts
GraphQLError.[toStringTag]

Constructors

Constructor

ts
new ForbiddenError(message, options?): ForbiddenError;

Defined in: packages/apollo/src/errors/forbidden.error.ts:26

Creates a new ForbiddenError.

Parameters

ParameterTypeDescription
messagestringError message.
options?GraphQLErrorOptionsAdditional GraphQL error options.

Returns

ForbiddenError

Overrides

ts
GraphQLError.constructor

Methods

toJSON()

ts
toJSON(): GraphQLFormattedError;

Defined in: node_modules/graphql/error/GraphQLError.d.ts:225

Returns the JSON representation used when this object is serialized.

Returns

GraphQLFormattedError

The JSON-serializable representation.

Example

ts
import { GraphQLError } from 'graphql/error';

const error = new GraphQLError('Resolver failed.', {
  path: ['viewer', 'name'],
  extensions: { code: 'INTERNAL' },
});

error.toJSON(); // => { message: 'Resolver failed.', path: ['viewer', 'name'], extensions: { code: 'INTERNAL' } }

Inherited from

ts
GraphQLError.toJSON

toString()

ts
toString(): string;

Defined in: node_modules/graphql/error/GraphQLError.d.ts:209

Returns this error as a human-readable message with source locations.

Returns

string

The formatted error string.

Example

ts
import { Source } from 'graphql/language';
import { GraphQLError } from 'graphql/error';

const error = new GraphQLError('Cannot query field "name".', {
  source: new Source('{ name }'),
  positions: [2],
});

error.toString(); // => 'Cannot query field "name".\n\nGraphQL request:1:3\n1 | { name }\n  |   ^'

Inherited from

ts
GraphQLError.toString

captureStackTrace()

Call Signature

ts
static captureStackTrace(targetObject, constructorOpt?): void;

Defined in: node_modules/@types/node/globals.d.ts:51

Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

js
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack;  // Similar to `new Error().stack`

The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

js
function a() {
  b();
}

function b() {
  c();
}

function c() {
  // Create an error without stack trace to avoid calculating the stack trace twice.
  const { stackTraceLimit } = Error;
  Error.stackTraceLimit = 0;
  const error = new Error();
  Error.stackTraceLimit = stackTraceLimit;

  // Capture the stack trace above function b
  Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
  throw error;
}

a();
Parameters
ParameterType
targetObjectobject
constructorOpt?Function
Returns

void

Inherited from
ts
GraphQLError.captureStackTrace

Call Signature

ts
static captureStackTrace(targetObject, constructorOpt?): void;

Defined in: node_modules/bun-types/globals.d.ts:1062

Create .stack property on a target object

Parameters
ParameterType
targetObjectobject
constructorOpt?Function
Returns

void

Inherited from
ts
GraphQLError.captureStackTrace

isError()

Call Signature

ts
static isError(error): error is Error;

Defined in: node_modules/typescript/lib/lib.esnext.error.d.ts:21

Indicates whether the argument provided is a built-in Error instance or not.

Parameters
ParameterType
errorunknown
Returns

error is Error

Inherited from
ts
GraphQLError.isError

Call Signature

ts
static isError(value): value is Error;

Defined in: node_modules/bun-types/globals.d.ts:1057

Check if a value is an instance of Error

Parameters
ParameterTypeDescription
valueunknownThe value to check
Returns

value is Error

True if the value is an instance of Error, false otherwise

Inherited from
ts
GraphQLError.isError

prepareStackTrace()

ts
static prepareStackTrace(err, stackTraces): any;

Defined in: node_modules/@types/node/globals.d.ts:55

Parameters

ParameterType
errError
stackTracesCallSite[]

Returns

any

See

https://v8.dev/docs/stack-trace-api#customizing-stack-traces

Inherited from

ts
GraphQLError.prepareStackTrace

Properties

PropertyModifierTypeDescriptionInherited fromDefined in
cause?publicunknownThe cause of the error.GraphQLError.causenode_modules/typescript/lib/lib.es2022.error.d.ts:24
coordinate?readonlystringAn optional schema coordinate (e.g. "MyType.myField") associated with this error.GraphQLError.coordinatenode_modules/@graphql-tools/utils/typings/errors.d.ts:19
extensionsreadonlyGraphQLErrorExtensionsExtension fields to add to the formatted error.GraphQLError.extensionsnode_modules/graphql/error/GraphQLError.d.ts:90
locationsreadonlyreadonly SourceLocation[] | undefinedAn array of { line, column } locations within the source GraphQL document which correspond to this error. Errors during validation often contain multiple locations, for example to point out two things with the same name. Errors during execution include a single location, the field which produced the error. Enumerable, and appears in the result of JSON.stringify().GraphQLError.locationsnode_modules/graphql/error/GraphQLError.d.ts:65
messagepublicstring-GraphQLError.messagenode_modules/typescript/lib/lib.es5.d.ts:1075
namepublicstring-GraphQLError.namenode_modules/typescript/lib/lib.es5.d.ts:1074
nodesreadonlyreadonly ASTNode[] | undefinedAn array of GraphQL AST Nodes corresponding to this error.GraphQLError.nodesnode_modules/graphql/error/GraphQLError.d.ts:74
originalErrorreadonlyError | undefinedOriginal error that caused this GraphQLError, if one exists.GraphQLError.originalErrornode_modules/graphql/error/GraphQLError.d.ts:88
pathreadonlyreadonly (string | number)[] | undefinedAn array describing the JSON-path into the execution response which corresponds to this error. Only included for errors during execution. Enumerable, and appears in the result of JSON.stringify().GraphQLError.pathnode_modules/graphql/error/GraphQLError.d.ts:72
positionsreadonlyreadonly number[] | undefinedAn array of character offsets within the source GraphQL document which correspond to this error.GraphQLError.positionsnode_modules/graphql/error/GraphQLError.d.ts:86
sourcereadonlySource | undefinedThe source GraphQL document for the first location of this error. Note that if this Error represents more than one node, the source may not represent nodes after the first node.GraphQLError.sourcenode_modules/graphql/error/GraphQLError.d.ts:81
stack?publicstring-GraphQLError.stacknode_modules/typescript/lib/lib.es5.d.ts:1076
stackTraceLimitstaticnumberThe Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)). The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames.GraphQLError.stackTraceLimitnode_modules/@types/node/globals.d.ts:67

Released under the MIT License.