/** * Copyright (c) 2020 TypeFox GmbH. All rights reserved. * Licensed under the GNU Affero General Public License (AGPL). * See License-AGPL.txt in the project root for license information. */ import { log, LogContext } from './logging'; export class SafePromise { public static catchAndLog(p: Promise, logCtx?: LogContext) { return SafePromise.catch(p, (err) => { if (logCtx) { log.error(logCtx, err); } else { log.error(err); } }); }; public static catch(p: Promise, handler: (err: any) => void): Promise { return p.catch((err) => { handler(err); return {} as T; // Nobody will ever see this value as the Promise already failed. It's just there to please the compiler }); }; }