1
0
mirror of synced 2026-05-22 22:53:20 +00:00
Files
music-metadata/lib/ParseError.ts
2025-04-01 19:57:20 +02:00

56 lines
1.7 KiB
TypeScript

export type UnionOfParseErrors =
| CouldNotDetermineFileTypeError
| UnsupportedFileTypeError
| UnexpectedFileContentError
| FieldDecodingError
| InternalParserError;
export const makeParseError = <Name extends string>(name: Name) => {
return class ParseError extends Error {
name: Name
constructor(message: string) {
super(message);
this.name = name;
}
}
}
// Concrete error class representing a file type determination failure.
export class CouldNotDetermineFileTypeError extends makeParseError('CouldNotDetermineFileTypeError') {
}
// Concrete error class representing an unsupported file type.
export class UnsupportedFileTypeError extends makeParseError('UnsupportedFileTypeError') {
}
// Concrete error class representing unexpected file content.
class UnexpectedFileContentError extends makeParseError('UnexpectedFileContentError') {
public readonly fileType: string;
constructor(fileType: string, message: string) {
super(message);
this.fileType = fileType;
}
// Override toString to include file type information.
toString(): string {
return `${this.name} (FileType: ${this.fileType}): ${this.message}`;
}
}
// Concrete error class representing a field decoding error.
export class FieldDecodingError extends makeParseError('FieldDecodingError') {
}
export class InternalParserError extends makeParseError('InternalParserError') {
}
// Factory function to create a specific type of UnexpectedFileContentError.
export const makeUnexpectedFileContentError = <FileType extends string>(fileType: FileType) => {
return class extends UnexpectedFileContentError {
constructor(message: string) {
super(fileType, message);
}
};
};