Changeset View
Changeset View
Standalone View
Standalone View
lib/utils/errors.js
// @flow | // @flow | ||||
import copyError from 'utils-copy-error'; | import copyError from 'utils-copy-error'; | ||||
import type { PlatformDetails } from '../types/device-types.js'; | import type { PlatformDetails } from '../types/device-types.js'; | ||||
import type { ServerSocketMessageType } from '../types/socket-types.js'; | |||||
class ExtendableError extends Error { | class ExtendableError extends Error { | ||||
constructor(message: string) { | constructor(message: string) { | ||||
super(message); | super(message); | ||||
this.name = this.constructor.name; | this.name = this.constructor.name; | ||||
this.message = message; | this.message = message; | ||||
if (typeof Error.captureStackTrace === 'function') { | if (typeof Error.captureStackTrace === 'function') { | ||||
Error.captureStackTrace(this, this.constructor); | Error.captureStackTrace(this, this.constructor); | ||||
Show All 22 Lines | class FetchTimeout extends ExtendableError { | ||||
url: string; | url: string; | ||||
constructor(error: string, url: string) { | constructor(error: string, url: string) { | ||||
super(error); | super(error); | ||||
this.url = url; | this.url = url; | ||||
} | } | ||||
} | } | ||||
class SocketOffline extends ExtendableError {} | |||||
class SocketTimeout extends ExtendableError { | |||||
expectedResponseType: ServerSocketMessageType; | |||||
constructor(expectedType: ServerSocketMessageType) { | |||||
super(`socket timed out waiting for response type ${expectedType}`); | |||||
this.expectedResponseType = expectedType; | |||||
} | |||||
} | |||||
function getMessageForException(e: mixed): ?string { | function getMessageForException(e: mixed): ?string { | ||||
if (typeof e === 'string') { | if (typeof e === 'string') { | ||||
return e; | return e; | ||||
} else if ( | } else if ( | ||||
e && | e && | ||||
typeof e === 'object' && | typeof e === 'object' && | ||||
e.message && | e.message && | ||||
typeof e.message === 'string' | typeof e.message === 'string' | ||||
) { | ) { | ||||
return e.message; | return e.message; | ||||
} | } | ||||
return undefined; | return undefined; | ||||
} | } | ||||
function cloneError<E>(e: E): E { | function cloneError<E>(e: E): E { | ||||
return copyError(e); | return copyError(e); | ||||
} | } | ||||
export { | export { | ||||
ExtendableError, | ExtendableError, | ||||
ServerError, | ServerError, | ||||
FetchTimeout, | FetchTimeout, | ||||
getMessageForException, | getMessageForException, | ||||
cloneError, | cloneError, | ||||
SocketOffline, | |||||
SocketTimeout, | |||||
}; | }; |