@sprucelabs/mercury-client
Version:
The simple way to interact with the Spruce Experience Platform
67 lines (66 loc) • 3.48 kB
JavaScript
import AbstractSpruceError from '@sprucelabs/error';
export default class SpruceError extends AbstractSpruceError {
friendlyMessage() {
var _a;
const { options } = this;
let message;
switch (options === null || options === void 0 ? void 0 : options.code) {
case 'INVALID_PROTOCOL':
message = `Unable to connect to Mercury using the uri you provided. The uri you provide was "${options.uri}"!`;
break;
case 'UNEXPECTED_PAYLOAD':
message = `You passed a payload to "${options.eventName}" that has no emit payload defined.`;
break;
case 'INVALID_PAYLOAD':
message = `The emit payload you passed to "${options.eventName}" is invalid${options.originalError
? `:\n\n${options.originalError.message}`
: '.'}`;
break;
case 'CONNECTION_FAILED':
message = `I couldn't connect to ${options.host}. The code I got back was ${options.statusCode}.`;
break;
case 'NOT_CONNECTED':
message = `You cannot ${options.action} after you have manually disconnected from Mercury. Event was '${options.fqen}'.`;
break;
case 'TIMEOUT': {
const retries = options.totalRetries
? ` ${options.totalRetries} times `
: ' ';
const each = options.totalRetries ? ' each ' : ' ';
message = `Dang it, I didn't hear back after emitting "${options.eventName}"${retries}for ${options.timeoutMs / 1000} seconds${each}...`;
if (options.isConnected === false) {
message +=
"\n\nAlso, it appears I'm not connected to the api.";
}
break;
}
case 'MISSING_TEST_CACHE_DIR':
message =
'You must set a test cache dir to test Mercury. Try MercuryFactory.setTestCacheDir().';
break;
case 'UNKNOWN_ERROR':
message = 'Oh no! An unknown error ocurred.';
if (options.originalError) {
message += ' Original error:\n\n';
message += options.originalError.stack;
}
return message;
case 'UNAUTHORIZED_ACCESS':
message = `Not authorized! You cannot ${options.action} \`${options.fqen}\`!`;
break;
case 'INVALID_EVENT_SIGNATURE':
message = `The signature for '${options.fqen}' is invalid:\n\n${options.instructions}`;
break;
case 'MUST_HANDLE_LOCALLY':
message = `'${options.fqen}' must be handled locally. You have 2 options:\n\n1. Make sure you have created a listener using 'spruce create.listener' and have booted your skill using 'await this.bootSkill()'\n\n2. Use eventFaker.on(...) to setup a listener in your test!`;
break;
case 'MUST_CREATE_EVENT':
message = `The event '${options.fqen}' was not found! That means it's time to create it with 'spruce create.event'.`;
break;
default:
message = super.friendlyMessage();
}
message = (_a = options.friendlyMessage) !== null && _a !== void 0 ? _a : message;
return message;
}
}