UNPKG

@iobroker/js-controller-common-db

Version:

The Library contains the common utils for the ioBroker controller which can be used by db classes too, as they do not rely on the db (circular dependencies).

59 lines 3.1 kB
// Beware, below be TypeScript dragons! import { ERRORS } from '../../lib/common/tools.js'; /** * Checks if the given callback is a function and if so calls it with the given parameter immediately, else a resolved Promise is returned * * @param callback - callback function to be executed * @param args - as many arguments as needed, which will be returned by the callback function or by the Promise * @returns if Promise is resolved with multiple arguments, an array is returned */ export function maybeCallback(callback, ...args) { if (typeof callback === 'function') { // if function we call it with given param setImmediate(callback, ...args); } else { return Promise.resolve(args.length > 1 ? args : args[0]); } } /** * Checks if the given callback is a function and if so calls it with the given error and parameter immediately, else a resolved or rejected Promise is returned. Error ERROR_DB_CLOSED are not rejecting the promise * * @param callback - callback function to be executed * @param error - error which will be used by the callback function. If callback is not a function and * error is given, a rejected Promise is returned. If error is given, but it is not an instance of Error, it is converted into one. * @param args - as many arguments as needed, which will be returned by the callback function or by the Promise * @returns if Promise is resolved with multiple arguments, an array is returned */ export function maybeCallbackWithError(callback, error, ...args) { if (error !== undefined && error !== null && !(error instanceof Error)) { // if it's not a real Error, we convert it into one error = new Error(error); } const isDbError = error ? error.message === ERRORS.ERROR_DB_CLOSED : false; if (typeof callback === 'function') { setImmediate(callback, error, ...args); } else if (error && !isDbError) { return Promise.reject(error); } else { return Promise.resolve(args.length > 1 ? args : args[0]); } } /** * Checks if the given callback is a function and if so calls it with the given error and parameter immediately, else a resolved or rejected Promise is returned. Redis-Error "Connection is closed." is converted into ERROR_DB_CLOSED * * @param callback - callback function to be executed * @param error - error which will be used by the callback function. If callback is not a function and * error is given, a rejected Promise is returned. If error is given, but it is not an instance of Error, it is converted into one. * @param args - as many arguments as needed, which will be returned by the callback function or by the Promise * @returns Promise if Promise is resolved with multiple arguments, an array is returned */ export function maybeCallbackWithRedisError(callback, error, ...args) { if (error instanceof Error && error.message.includes('Connection is closed')) { error.message = ERRORS.ERROR_DB_CLOSED; } return maybeCallbackWithError(callback, error, ...args); } //# sourceMappingURL=maybeCallback.js.map