import Boom from '@hapi/boom' ; import { get } from 'lodash' ; const code = Symbol ( 'OpenSearchError' ) ; enum ErrorCode { NOT_AUTHORIZED = 'OpenSearch/notAuthorized' } deprecated public export interface LegacyOpenSearchError extends Boom . Boom { [ code ] ? : string ; } function isOpenSearchError ( error ) { return Boolean ( error && error [ code ] ) ; } function decorate ( error , errorCode , statusCode , message ? ) { if ( isOpenSearchError ( error ) ) { return error ; } const boom = Boom . boomify ( error , { statusCode , message , override : false , } ) as LegacyOpenSearchError ; boom [ code ] = errorCode ; return boom ; } public example export class LegacyOpenSearchErrorHelpers { public static isNotAuthorizedError ( error ) { return isOpenSearchError ( error ) && error [ code ] === ErrorCode . NOT_AUTHORIZED ; } public static decorateNotAuthorizedError ( error , reason ? ) { const decoratedError = decorate ( error , ErrorCode . NOT_AUTHORIZED , 401 , reason ) ; const wwwAuthHeader = get ( error , 'body.error.header[WWW-Authenticate]' ) as string ; ( decoratedError . output . headers as { [ key ] : string ; } ) [ 'WWW-Authenticate' ] = wwwAuthHeader || 'Basic realm="Authorization Required"' ; return decoratedError ; } }