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

fix: handle single error responses #49

Merged
merged 2 commits into from
Jan 24, 2024
Merged
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
8 changes: 7 additions & 1 deletion src/knock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,13 @@ class Knock {
throw new BadRequestException(code, message, requestID);
}
case 422: {
const { errors } = data;
// Format errors as an array before passing to exception constructor
const errors = !data.errors
? []
: Array.isArray(data.errors)
? data.errors
: [data.errors];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If errors is null or undefined, then won't this end up as [null]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, added an extra check if errors is not defined


throw new UnprocessableEntityException(errors, requestID);
}
case 404: {
Expand Down