import {MessageCommandErrorResult} from "./Messages"; import {clientServiceLogger} from "./Logging"; export type ActionResult = { unwrap() : T; } & ({ status: "success", result: T } | { status: "error", result: MessageCommandErrorResult }); export function createErrorResult(result: MessageCommandErrorResult) : ActionResult { return { status: "error", result: result, unwrap(): T { clientServiceLogger.logError("Tried to unwrap an action which failed: %o", result); throw "action failed with " + result.type; } } } export function createResult() : ActionResult; export function createResult(result: T) : ActionResult; export function createResult(result?) : ActionResult { return { status: "success", result: result, unwrap() { return result; } } }