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

Error Handling #114

Closed
betocantu93 opened this issue May 4, 2019 · 1 comment
Closed

Error Handling #114

betocantu93 opened this issue May 4, 2019 · 1 comment

Comments

@betocantu93
Copy link

betocantu93 commented May 4, 2019

Currently if an operation fails because of a database constraint or what ever, I can't handle them gracefully

async add(op: Operation): Promise<any> {
    const { password, ...user } = op.data.attributes;

    if (!password) {
      throw {
        // At the very least, you must declare a status and a code.
        status: HttpStatusCode.BadRequest,
        title: "A password must be entered.",
        code: "no_password"
      } as JsonApiError;
    }

    user.salt = uuid()
    user.hashedPassword = hash(password, user.salt);

    op.data.attributes = user;
    try {
      return super.add({ ...op, params: {} });
    } catch (e) {
      throw {
        status: HttpStatusCode.BadRequest,
        title: "the email already exists",
        code: "duplicated_user"
      } as JsonApiError;
    }
  }

This is what I would like/expect, but I don't know if its the best approach.

@betocantu93
Copy link
Author

I actually got it working, I just had to await for the super there.

 try {
      return await super.add({ ...op, params: {} });
    } catch (e) {
      throw {
        status: HttpStatusCode.BadRequest,
        title: e.detail,
        code: e.constraint
      } as JsonApiError;
    }

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