@anam-ai/js-sdk
Version:
Client side JavaScript SDK for Anam AI
61 lines • 2.92 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClientError = exports.sendErrorMetric = exports.ErrorCode = void 0;
var ErrorCode;
(function (ErrorCode) {
ErrorCode["CLIENT_ERROR_CODE_USAGE_LIMIT_REACHED"] = "CLIENT_ERROR_CODE_USAGE_LIMIT_REACHED";
ErrorCode["CLIENT_ERROR_CODE_VALIDATION_ERROR"] = "CLIENT_ERROR_CODE_VALIDATION_ERROR";
ErrorCode["CLIENT_ERROR_CODE_AUTHENTICATION_ERROR"] = "CLIENT_ERROR_CODE_AUTHENTICATION_ERROR";
ErrorCode["CLIENT_ERROR_CODE_SERVER_ERROR"] = "CLIENT_ERROR_CODE_SERVER_ERROR";
ErrorCode["CLIENT_ERROR_CODE_MAX_CONCURRENT_SESSIONS_REACHED"] = "CLIENT_ERROR_CODE_MAX_CONCURRENT_SESSIONS_REACHED";
ErrorCode["CLIENT_ERROR_CODE_SERVICE_BUSY"] = "CLIENT_ERROR_CODE_SERVICE_BUSY";
ErrorCode["CLIENT_ERROR_CODE_NO_PLAN_FOUND"] = "CLIENT_ERROR_CODE_NO_PLAN_FOUND";
ErrorCode["CLIENT_ERROR_CODE_UNKNOWN_ERROR"] = "CLIENT_ERROR_CODE_UNKNOWN_ERROR";
})(ErrorCode || (exports.ErrorCode = ErrorCode = {}));
// TODO: Move to CoreApiRestClient if we have a pattern for not exposing this
const sendErrorMetric = (name, value, tags) => __awaiter(void 0, void 0, void 0, function* () {
try {
// TODO: Don't send this in dev
yield fetch('https://api.anam.ai/v1/metrics/client', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
name,
value,
tags,
}),
});
}
catch (error) {
console.error('Failed to send error metric:', error);
}
});
exports.sendErrorMetric = sendErrorMetric;
class ClientError extends Error {
constructor(message, code, statusCode = 500, details) {
super(message);
this.name = 'ClientError';
this.code = code;
this.statusCode = statusCode;
this.details = details;
Object.setPrototypeOf(this, ClientError.prototype);
// Send error metric when error is created
(0, exports.sendErrorMetric)('client_error', code, {
details,
statusCode,
});
}
}
exports.ClientError = ClientError;
//# sourceMappingURL=ClientError.js.map
;