2021-01-07 16:41:46 +00:00
|
|
|
// Preserves Binary codec.
|
|
|
|
|
2021-03-11 13:43:06 +00:00
|
|
|
import { Position } from "./annotated";
|
|
|
|
|
2021-01-07 16:41:46 +00:00
|
|
|
export type ErrorType = 'DecodeError' | 'EncodeError' | 'ShortPacket';
|
2021-01-11 15:54:52 +00:00
|
|
|
export const ErrorType = Symbol.for('ErrorType');
|
2021-01-07 16:41:46 +00:00
|
|
|
|
|
|
|
export abstract class PreservesCodecError {
|
2021-01-11 15:54:52 +00:00
|
|
|
abstract get [ErrorType](): ErrorType;
|
|
|
|
|
|
|
|
static isCodecError(e: any, t: ErrorType): e is PreservesCodecError {
|
|
|
|
return (e?.[ErrorType] === t);
|
|
|
|
}
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class DecodeError extends Error {
|
2021-03-11 13:43:06 +00:00
|
|
|
readonly pos: Position | undefined;
|
|
|
|
|
2021-01-11 15:54:52 +00:00
|
|
|
get [ErrorType](): ErrorType { return 'DecodeError' }
|
|
|
|
|
2021-03-11 13:43:06 +00:00
|
|
|
constructor(message: string, pos?: Position) {
|
|
|
|
super(message);
|
|
|
|
this.pos = pos;
|
|
|
|
}
|
|
|
|
|
2021-01-11 15:54:52 +00:00
|
|
|
static isDecodeError(e: any): e is DecodeError {
|
|
|
|
return PreservesCodecError.isCodecError(e, 'DecodeError');
|
|
|
|
}
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class EncodeError extends Error {
|
2021-01-11 15:54:52 +00:00
|
|
|
get [ErrorType](): ErrorType { return 'EncodeError' }
|
|
|
|
|
|
|
|
static isEncodeError(e: any): e is EncodeError {
|
|
|
|
return PreservesCodecError.isCodecError(e, 'EncodeError');
|
|
|
|
}
|
2021-01-07 16:41:46 +00:00
|
|
|
|
|
|
|
readonly irritant: any;
|
|
|
|
|
|
|
|
constructor(message: string, irritant: any) {
|
|
|
|
super(message);
|
|
|
|
this.irritant = irritant;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ShortPacket extends DecodeError {
|
2021-01-11 15:54:52 +00:00
|
|
|
get [ErrorType](): ErrorType { return 'ShortPacket' }
|
|
|
|
|
|
|
|
static isShortPacket(e: any): e is ShortPacket {
|
|
|
|
return PreservesCodecError.isCodecError(e, 'ShortPacket');
|
|
|
|
}
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|