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

feat: add ValidationException #694

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

marcogrcr
Copy link
Contributor

WHAT?

Add ValidationException and throw it when signRequest() or verifyRequest() fail validation.

WHY?

When there's a validation error a runtypes.ValidationError is thrown. This is an internal implementation detail and as such, there is no supported way of detecting validation errors.

Similar to #692, so that developers can:

import { ValidationException, verifyRequest } from '@contentful/node-apps-toolkit'

try {
  verifyRequest(/* ...*/)
} catch (e) {
  if (e instanceof ValidationException) {
    if (e.constraintName === 'SecretLength') {
        // this is unexpected, return 500 status code and alarm
    } else {
        // this is expected, should return 400 or 403 status code
    }
  }
}

WHAT?

Add `ValidationException` and throw it when `signRequest()` or
`verifyRequest()` fail validation.

WHY?

When there's a validation error a `runtypes.ValidationError` is thrown.
This is an internal implementation detail and as such, there is no
supported way of detecting validation errors.

Similar to [contentful#692], so that developers can:

```ts
import { ValidationException, verifyRequest } from '@contentful/node-apps-toolkit'

try {
  verifyRequest(/* ...*/)
} catch (e) {
  if (e instanceof ValidationException) {
    if (e.constraintName === 'SecretLength') {
        // this is unexpected, return 500 status code and alarm
    } else {
        // this is expected, should return 400 or 403 status code
    }
  }
}
```

[contentful#692]: contentful#692
@marcogrcr marcogrcr requested a review from a team as a code owner August 27, 2024 19:18
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: This is the same as src/typings/validators.ts. The only difference is that all objects are wrapped in proxyValidationError().

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

Successfully merging this pull request may close these issues.

1 participant