fix(chat): lower unavailable media logs
This commit is contained in:
+22
-5
@@ -17,6 +17,14 @@ export type Result<T> =
|
||||
/** Result.wrap 专用 logger —— 自动带 component: "Result" 标签 */
|
||||
const log = new Logger("Result");
|
||||
|
||||
type ResultErrorLogLevel = "info" | "error";
|
||||
|
||||
export interface ResultWrapOptions {
|
||||
errorLogLevel?:
|
||||
| ResultErrorLogLevel
|
||||
| ((error: Error) => ResultErrorLogLevel);
|
||||
}
|
||||
|
||||
export const Result = {
|
||||
/** 构造成功结果。 */
|
||||
ok<T>(data: T): Result<T> {
|
||||
@@ -56,15 +64,24 @@ export const Result = {
|
||||
* 3. 不重新抛出 —— 调用方拿到的永远是 `Result<T>`(throw 风格调用方可在
|
||||
* 最外层显式 `throw r.error`)
|
||||
*/
|
||||
async wrap<T>(thunk: () => Promise<T>): Promise<Result<T>> {
|
||||
async wrap<T>(
|
||||
thunk: () => Promise<T>,
|
||||
options?: ResultWrapOptions,
|
||||
): Promise<Result<T>> {
|
||||
try {
|
||||
return Result.ok(await thunk());
|
||||
} catch (e) {
|
||||
const err = toError(e);
|
||||
log.error(
|
||||
{ err, stack: err.stack, type: err.constructor.name },
|
||||
"Result.wrap caught exception",
|
||||
);
|
||||
const logLevel =
|
||||
typeof options?.errorLogLevel === "function"
|
||||
? options.errorLogLevel(err)
|
||||
: options?.errorLogLevel ?? "error";
|
||||
const details = { err, stack: err.stack, type: err.constructor.name };
|
||||
if (logLevel === "info") {
|
||||
log.info(details, "Result.wrap caught exception");
|
||||
} else {
|
||||
log.error(details, "Result.wrap caught exception");
|
||||
}
|
||||
return Result.err(err);
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user