UNPKG

unleash-server

Version:

Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.

59 lines 2.65 kB
import joi from 'joi'; import { UnleashError } from '../error/unleash-error.js'; import { fromLegacyError } from '../error/from-legacy-error.js'; import createError from 'http-errors'; export const customJoi = joi.extend((j) => ({ type: 'isUrlFriendly', base: j.string(), messages: { 'isUrlFriendly.base': '{{#label}} must be URL friendly', }, validate(value, helpers) { // Base validation regardless of the rules applied if (encodeURIComponent(value) !== value || value === '..' || value === '.') { // Generate an error, state and options need to be passed return { value, errors: helpers.error('isUrlFriendly.base') }; } return undefined; }, })); export const nameType = customJoi.isUrlFriendly().min(1).max(100).required(); export const handleErrors = (res, logger, error) => { if (createError.isHttpError(error)) { return res .status( // @ts-expect-error - The error object here is not guaranteed to contain status error.status ?? 400) .json({ message: error.message }); } const finalError = error instanceof UnleashError ? error : fromLegacyError(error); const format = (thing) => JSON.stringify(thing, null, 2); if (!(error instanceof UnleashError)) { logger.debug(`I encountered an error that wasn't an instance of the \`UnleashError\` type. The original error was: ${format(error)}. It was mapped to ${format(finalError.toJSON())}`); } if (finalError.statusCode === 500) { logger.error(`Server failed executing request: ${format(error)}`, error); } // details property behaves weirdly. Trying to access it as finalError.details[0], // hangs the execution of this method. Returning it as finalError.details doesn't // work returning undefined. Printing out the finalError object using JSON.stringify // shows that the details property is there and is an array. // Running JSON.stringify(finalError.details) also hangs. // As a workaround, we do a roundabout way of getting to the details property // by doing JSON.parse(JSON.stringify(finalError))['details'] const details = // @ts-expect-error - details might not be present on all UnleashErrors // biome-ignore lint/complexity/useLiteralKeys: see above finalError.details ?? JSON.parse(JSON.stringify(finalError))['details']; return res .status(finalError.statusCode) .json({ name: finalError.name, message: finalError.message, details, }) .end(); }; //# sourceMappingURL=util.js.map