@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
100 lines • 3.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExceptionUnpacker = exports.ensureError = exports.ResourceTypeNotFoundCmsException = exports.ResourceNotFoundCmsException = exports.CmsException = void 0;
const async_parallel_1 = require("async-parallel");
const exceptions_1 = require("../util/exceptions");
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 async_parallel_1.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;
}
}
exports.CmsException = CmsException;
class ResourceNotFoundCmsException extends CmsException {
constructor(resourceId, reason) {
super('CMS resource not found', reason, resourceId);
this.resourceId = resourceId;
this.reason = reason;
}
}
exports.ResourceNotFoundCmsException = ResourceNotFoundCmsException;
class ResourceTypeNotFoundCmsException extends CmsException {
constructor(resourceType, reason) {
super(`CMS resource type '${resourceType}' not found`, reason, undefined);
this.resourceType = resourceType;
this.reason = reason;
}
}
exports.ResourceTypeNotFoundCmsException = ResourceTypeNotFoundCmsException;
function ensureError(e) {
if ((0, exceptions_1.isError)(e)) {
return e;
}
return new CmsException(String(e), undefined);
}
exports.ensureError = ensureError;
class ExceptionUnpacker {
constructor(indent = ' ', prependSubErrorIndex = true) {
this.indent = indent;
this.prependSubErrorIndex = prependSubErrorIndex;
}
unpack(e) {
return this.processException(ensureError(e));
}
getMultiError(e) {
if (e instanceof async_parallel_1.MultiError) {
return e;
}
if (e instanceof CmsException && e.reason instanceof async_parallel_1.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]);
}
}
}
exports.ExceptionUnpacker = ExceptionUnpacker;
//# sourceMappingURL=exceptions.js.map