contentful / node-apps-toolkit

A collection of helpers and utilities for creating NodeJS Contentful Apps
MIT License
13 stars 5 forks source link

feat: add ValidationException #694

Open marcogrcr opened 2 months ago

marcogrcr commented 2 months ago

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
    }
  }
}