32 lines
1.2 KiB
JavaScript
32 lines
1.2 KiB
JavaScript
import { redactText } from './credentials.mjs';
|
|
|
|
export class BridgeError extends Error {
|
|
constructor(message, category = 'failed', {retryable = false, dispatched = false} = {}) {
|
|
super(redactText(String(message ?? '本机操作未完成')));
|
|
this.category=category;
|
|
this.retryable=retryable;
|
|
this.dispatched=dispatched;
|
|
}
|
|
|
|
wire() {
|
|
const {category,retryable,dispatched,message}=this;
|
|
return {category,retryable,dispatched,message};
|
|
}
|
|
|
|
static from(value) {
|
|
if(value instanceof BridgeError)return value;
|
|
return typeof value==='object' && value!==null
|
|
? new BridgeError(value.message,value.category,value) : new BridgeError(value);
|
|
}
|
|
}
|
|
|
|
export const stoppedCanvas = error =>
|
|
['scope_changed','auth_revoked','write_revoked','revision_conflict','result_unknown','cancelled','transport_interrupted'].includes(error?.category);
|
|
|
|
export const authorityLost = error => ['scope_changed', 'auth_revoked'].includes(error?.category);
|
|
|
|
export function authorityFailure(category, options) {
|
|
return new BridgeError(category === 'scope_changed' ? '账户已改变,原项目和原请求已保留;请切回原账户'
|
|
: '授权已失效,制作已暂停;请重新登录', category, options);
|
|
}
|