-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement own custom
Error
using ADT style implementation
- Loading branch information
Showing
33 changed files
with
238 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
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') { | ||
constructor(public readonly fileType: string, message: string) { | ||
super(message); | ||
} | ||
|
||
// 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); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.