import Boom from '@hapi/boom' ; const CODE_BAD_REQUEST = 'SavedObjectsClient/badRequest' ; const CODE_INVALID_VERSION = 'SavedObjectsClient/invalidVersion' ; const CODE_NOT_AUTHORIZED = 'SavedObjectsClient/notAuthorized' ; const CODE_FORBIDDEN = 'SavedObjectsClient/forbidden' ; const CODE_REQUEST_ENTITY_TOO_LARGE = 'SavedObjectsClient/requestEntityTooLarge' ; const CODE_NOT_FOUND = 'SavedObjectsClient/notFound' ; const CODE_CONFLICT = 'SavedObjectsClient/conflict' ; const CODE_TOO_MANY_REQUESTS = 'SavedObjectsClient/tooManyRequests' ; const CODE_ES_CANNOT_EXECUTE_SCRIPT = 'SavedObjectsClient/esCannotExecuteScript' ; const CODE_ES_UNAVAILABLE = 'SavedObjectsClient/esUnavailable' ; const CODE_GENERAL_ERROR = 'SavedObjectsClient/generalError' ; const code = Symbol ( 'SavedObjectsClientErrorCode' ) ; export interface DecoratedError extends Boom . Boom { [ code ] ? : string ; } function decorate ( error , errorCode , statusCode , message ? ) { if ( isSavedObjectsClientError ( error ) ) { return error ; } const boom = Boom . boomify ( error , { statusCode , message , override : false , } ) as DecoratedError ; boom [ code ] = errorCode ; return boom ; } function isSavedObjectsClientError ( error ) { return Boolean ( error && error [ code ] ) ; } function decorateBadRequestError ( error , reason ? ) { return decorate ( error , CODE_BAD_REQUEST , 400 , reason ) ; } public export class SavedObjectsErrorHelpers { public static isSavedObjectsClientError ( error ) { return isSavedObjectsClientError ( error ) ; } public static decorateBadRequestError ( error , reason ? ) { return decorateBadRequestError ( error , reason ) ; } public static createBadRequestError ( reason ? ) { return decorateBadRequestError ( new Error ( 'Bad Request' ) , reason ) ; } public static createUnsupportedTypeError ( type ) { return decorateBadRequestError ( new Error ( 'Bad Request' ) , `Unsupported saved object type: '${ type }'` ) ; } public static isBadRequestError ( error ) { return isSavedObjectsClientError ( error ) && error [ code ] === CODE_BAD_REQUEST ; } public static createInvalidVersionError ( versionInput ? ) { return decorate ( Boom . badRequest ( `Invalid version [${ versionInput }]` ) , CODE_INVALID_VERSION , 400 ) ; } public static isInvalidVersionError ( error ) { return isSavedObjectsClientError ( error ) && error [ code ] === CODE_INVALID_VERSION ; } public static decorateNotAuthorizedError ( error , reason ? ) { return decorate ( error , CODE_NOT_AUTHORIZED , 401 , reason ) ; } public static isNotAuthorizedError ( error ) { return isSavedObjectsClientError ( error ) && error [ code ] === CODE_NOT_AUTHORIZED ; } public static decorateForbiddenError ( error , reason ? ) { return decorate ( error , CODE_FORBIDDEN , 403 , reason ) ; } public static isForbiddenError ( error ) { return isSavedObjectsClientError ( error ) && error [ code ] === CODE_FORBIDDEN ; } public static decorateRequestEntityTooLargeError ( error , reason ? ) { return decorate ( error , CODE_REQUEST_ENTITY_TOO_LARGE , 413 , reason ) ; } public static isRequestEntityTooLargeError ( error ) { return isSavedObjectsClientError ( error ) && error [ code ] === CODE_REQUEST_ENTITY_TOO_LARGE ; } public static createGenericNotFoundError ( type = null , id = null ) { if ( type && id ) { return decorate ( Boom . notFound ( `Saved object [${ type }/${ id }] not found` ) , CODE_NOT_FOUND , 404 ) ; } return decorate ( Boom . notFound ( ) , CODE_NOT_FOUND , 404 ) ; } public static createIndexAliasNotFoundError ( alias ) { return SavedObjectsErrorHelpers . decorateIndexAliasNotFoundError ( Boom . internal ( ) , alias ) ; } public static decorateIndexAliasNotFoundError ( error , alias ) { return decorate ( error , CODE_GENERAL_ERROR , 500 , `Saved object index alias [${ alias }] not found` ) ; } public static isNotFoundError ( error ) { return isSavedObjectsClientError ( error ) && error [ code ] === CODE_NOT_FOUND ; } public static decorateConflictError ( error , reason ? ) { return decorate ( error , CODE_CONFLICT , 409 , reason ) ; } public static createConflictError ( type , id , reason ? ) { return SavedObjectsErrorHelpers . decorateConflictError ( Boom . conflict ( `Saved object [${ type }/${ id }] conflict` ) , reason ) ; } public static isConflictError ( error ) { return isSavedObjectsClientError ( error ) && error [ code ] === CODE_CONFLICT ; } public static decorateTooManyRequestsError ( error , reason ? ) { return decorate ( error , CODE_TOO_MANY_REQUESTS , 429 , reason ) ; } public static createTooManyRequestsError ( type , id ) { return SavedObjectsErrorHelpers . decorateTooManyRequestsError ( Boom . tooManyRequests ( ) ) ; } public static isTooManyRequestsError ( error ) { return isSavedObjectsClientError ( error ) && error [ code ] === CODE_TOO_MANY_REQUESTS ; } public static decorateEsCannotExecuteScriptError ( error , reason ? ) { return decorate ( error , CODE_ES_CANNOT_EXECUTE_SCRIPT , 400 , reason ) ; } public static isEsCannotExecuteScriptError ( error ) { return isSavedObjectsClientError ( error ) && error [ code ] === CODE_ES_CANNOT_EXECUTE_SCRIPT ; } public static decorateEsUnavailableError ( error , reason ? ) { return decorate ( error , CODE_ES_UNAVAILABLE , 503 , reason ) ; } public static isEsUnavailableError ( error ) { return isSavedObjectsClientError ( error ) && error [ code ] === CODE_ES_UNAVAILABLE ; } public static decorateGeneralError ( error , reason ? ) { return decorate ( error , CODE_GENERAL_ERROR , 500 , reason ) ; } public static isGeneralError ( error ) { return isSavedObjectsClientError ( error ) && error [ code ] === CODE_GENERAL_ERROR ; } public static createGenericNotFoundEsUnavailableError ( type = null , id = null ) { const notFoundError = this . createGenericNotFoundError ( type , id ) ; return this . decorateEsUnavailableError ( new Error ( `${ notFoundError . message }` ) , `x-elastic-product not present or not recognized` ) ; } }