Skip to content
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

[Question] Custom Errors by extending ApolloError #613

Closed
hitech95 opened this issue May 28, 2020 · 0 comments
Closed

[Question] Custom Errors by extending ApolloError #613

hitech95 opened this issue May 28, 2020 · 0 comments

Comments

@hitech95
Copy link

I've tried to create my own error to throw when login fails too many times:

import { ApolloError } from "apollo-server-errors/src/index"

export class AuthLockdownError extends ApolloError {
  constructor(message: string, properties?: Record<string, any>) {
    super(message, 'AUTH_LOCKDOWN', properties);

    Object.defineProperty(this, 'name', { value: 'AuthLockdownError' });
  }
}

Usage:

 // Handle a login request to the application.
  async login(requestIP: string, data: { email: string; password: string }): Promise<User> {
    ...

    if (AUTH_THROTTLE && await this.hasTooManyLoginAttempts(throttleKey)) {
      throw new AuthLockdownError("Too many loin attempts have failed")
    }

    const isValidPassword = await bcrypt.compare(data.password, user.password)
    ....
  }

When doing so I get the following error

(node:7284) UnhandledPromiseRejectionWarning: D:\graphql-tests\typescript-boilerplate\node_modules\apollo-server-errors\src\index.ts:1
import { GraphQLError, GraphQLFormattedError } from 'graphql';
^^^^^^

SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:1070:16)
at Module._compile (internal/modules/cjs/loader.js:1120:27)
at Module._compile (D:\graphql-tests\typescript-boilerplate\node_modules\source-map-support\source-map-support.js:521:25)
at Module.m._compile (C:\Users\Hitech95\AppData\Local\Temp\ts-node-dev-hook-8290200771403802.js:57:25)
at Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at require.extensions. (C:\Users\Hitech95\AppData\Local\Temp\ts-node-dev-hook-8290200771403802.js:59:14)
at Object.nodeDevHook [as .ts] (D:\graphql-tests\typescript-boilerplate\node_modules\ts-node-dev\lib\hook.js:61:7)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Module.require (internal/modules/cjs/loader.js:1042:19)

(node:7284) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To term

I'm quite new with TS/Node but I don't understand why is giving me this issue on a module instead of on my code. Reading online looks like that it have to do with the tsconfig.ts file.

Any suggestyion on how to solve this using this boilerplate?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant