UNPKG

@botonic/plugin-contentful

Version:

Botonic Plugin Contentful is one of the **[available](https://github.com/hubtype/botonic/tree/master/packages)** plugins for Botonic. **[Contentful](http://www.contentful.com)** is a CMS (Content Management System) which manages contents of a great variet

92 lines 3.16 kB
import { MultiError } from 'async-parallel'; import { isError } from '../util/exceptions'; export class CmsException extends Error { /** * @param message description of the problem * @param reason what caused the exception (normally a low level exception) */ constructor(message, reason, resourceId) { super(CmsException.mergeMessages(message, reason, resourceId)); this.reason = reason; this.resourceId = resourceId; } messageFromReason() { if (!this.reason) { return undefined; } if (this.reason instanceof MultiError) { return this.reason.list.map(e => e.message).join('\n'); } if (this.reason instanceof Error) { return this.reason.message; } return String(this.reason); } /** * Reason's string is merged into message because many tools (eg. jest) * only report Error.message and not Error.toString() */ static mergeMessages(message, reason, resourceId) { // resourceId already reported by ErrorReportingCMS, but not yet // for contents & topContents methods if (resourceId && !message.includes(resourceId.id)) { message += ` on content ${resourceId.toString()}`; } // eslint-disable-next-line @typescript-eslint/restrict-template-expressions if (reason) { message += `. ${String(reason.message || reason)}`; } return message; } } export class ResourceNotFoundCmsException extends CmsException { constructor(resourceId, reason) { super('CMS resource not found', reason, resourceId); this.resourceId = resourceId; this.reason = reason; } } export class ResourceTypeNotFoundCmsException extends CmsException { constructor(resourceType, reason) { super(`CMS resource type '${resourceType}' not found`, reason, undefined); this.resourceType = resourceType; this.reason = reason; } } export function ensureError(e) { if (isError(e)) { return e; } return new CmsException(String(e), undefined); } export class ExceptionUnpacker { constructor(indent = ' ', prependSubErrorIndex = true) { this.indent = indent; this.prependSubErrorIndex = prependSubErrorIndex; } unpack(e) { return this.processException(ensureError(e)); } getMultiError(e) { if (e instanceof MultiError) { return e; } if (e instanceof CmsException && e.reason instanceof MultiError) { return e.reason; } return undefined; } processException(e, index) { const multiError = this.getMultiError(e); if (multiError) { const msgLists = multiError.list.map((e1, i) => this.processException(e1, i + 1)); const flat = Array.prototype.concat(...msgLists); return flat; } else { const indexPre = index ? [`${this.indent}${index}:`] : []; return indexPre.concat([this.indent + e.message]); } } } //# sourceMappingURL=exceptions.js.map