flipper-common
Version:
Server & UI shared Flipper utilities
164 lines • 5.46 kB
JavaScript
;
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.deserializeRemoteError = exports.getStringFromErrorLike = exports.getErrorFromErrorLike = exports.isError = exports.FlipperServerTimeoutError = exports.FlipperServerDisconnectedError = exports.NoLongerConnectedToClientError = exports.UserNotSignedInError = exports.X2PAgentdError = exports.UserUnauthorizedError = exports.InternGraphServerError = exports.ConnectivityError = exports.CancelledPromiseError = exports.UnableToExtractClientQueryError = exports.UserError = exports.SystemError = exports.isConnectivityOrAuthError = exports.isAuthError = void 0;
function isAuthError(err) {
return (err instanceof UserNotSignedInError || err instanceof UserUnauthorizedError);
}
exports.isAuthError = isAuthError;
function isConnectivityOrAuthError(err) {
return (err instanceof ConnectivityError ||
err instanceof X2PAgentdError ||
isAuthError(err) ||
String(err).startsWith('Failed to fetch') ||
// In cases where the error message is wrapped but the
// underlying core issue is still a fetch failure.
String(err).endsWith('Failed to fetch'));
}
exports.isConnectivityOrAuthError = isConnectivityOrAuthError;
class SystemError extends Error {
constructor(msg, ...args) {
super(msg);
this.name = 'SystemError';
this.context = args;
}
}
exports.SystemError = SystemError;
class UserError extends Error {
constructor(msg, ...args) {
super(msg);
this.name = 'UserError';
this.context = args;
}
}
exports.UserError = UserError;
class UnableToExtractClientQueryError extends Error {
constructor(msg) {
super(msg);
this.name = 'UnableToExtractClientQueryError';
}
}
exports.UnableToExtractClientQueryError = UnableToExtractClientQueryError;
class CancelledPromiseError extends Error {
constructor(msg) {
super(msg);
this.name = 'CancelledPromiseError';
}
}
exports.CancelledPromiseError = CancelledPromiseError;
class ConnectivityError extends Error {
constructor(msg) {
super(msg);
this.name = 'ConnectivityError';
}
}
exports.ConnectivityError = ConnectivityError;
class InternGraphServerError extends Error {
constructor(msg, body) {
super(msg);
this.name = 'InternGraphServerError';
this.body = body;
}
}
exports.InternGraphServerError = InternGraphServerError;
class UserUnauthorizedError extends Error {
constructor(msg = 'User unauthorized.') {
super(msg);
this.name = 'UserUnauthorizedError';
}
}
exports.UserUnauthorizedError = UserUnauthorizedError;
class X2PAgentdError extends Error {
constructor(msg) {
super(msg);
this.name = 'X2PAgentdError';
}
}
exports.X2PAgentdError = X2PAgentdError;
class UserNotSignedInError extends Error {
constructor(msg = 'User not signed in.') {
super(msg);
this.name = 'UserNotSignedInError';
}
}
exports.UserNotSignedInError = UserNotSignedInError;
class NoLongerConnectedToClientError extends Error {
constructor(msg = 'No longer connected to client.') {
super(msg);
this.name = 'NoLongerConnectedToClientError';
}
}
exports.NoLongerConnectedToClientError = NoLongerConnectedToClientError;
class FlipperServerDisconnectedError extends Error {
constructor(reason) {
super(`Flipper Server disconnected. Reason: ${reason}`);
this.reason = reason;
}
}
exports.FlipperServerDisconnectedError = FlipperServerDisconnectedError;
class FlipperServerTimeoutError extends Error {
constructor(msg) {
super(`Flipper Server timeout. Reason: ${msg}`);
}
}
exports.FlipperServerTimeoutError = FlipperServerTimeoutError;
function isError(obj) {
return (obj instanceof Error ||
(obj &&
obj.name &&
typeof obj.name === 'string' &&
obj.message &&
typeof obj.message === 'string' &&
obj.stack &&
typeof obj.stack === 'string'));
}
exports.isError = isError;
function getErrorFromErrorLike(e) {
if (Array.isArray(e)) {
return e.map(getErrorFromErrorLike).find((x) => x);
}
else if (isError(e)) {
return e;
}
else {
return undefined;
}
}
exports.getErrorFromErrorLike = getErrorFromErrorLike;
function getStringFromErrorLike(e) {
if (Array.isArray(e)) {
return e.map(getStringFromErrorLike).join(' ');
}
else if (typeof e == 'string') {
return e;
}
else if (isError(e)) {
return e.message || e.toString();
}
else {
try {
return JSON.stringify(e);
}
catch (e) {
// Stringify might fail on arbitrary structures
// Last resort: toString it.
return `${e}`;
}
}
}
exports.getStringFromErrorLike = getStringFromErrorLike;
const deserializeRemoteError = (serializedError) => {
const err = new Error(serializedError.message);
err.name = serializedError.name;
err.stack += `. Caused by: ${serializedError.stacktrace}`;
return err;
};
exports.deserializeRemoteError = deserializeRemoteError;
//# sourceMappingURL=errors.js.map