Skip to content

Instantly share code, notes, and snippets.

@sshine
Last active November 4, 2021 17:48
Show Gist options
  • Save sshine/1b4f0fdbb1660c08921faaff5d03fe79 to your computer and use it in GitHub Desktop.
Save sshine/1b4f0fdbb1660c08921faaff5d03fe79 to your computer and use it in GitHub Desktop.
export type ClaErrorType = {
message: string;
extensions: ClaErrorTypeExtension[];
locations: ClaErrorTypeLineColumn[];
};
export type ClaErrorTypeExtension = {
category: string;
};
export type ClaErrorTypeLineColumn = {
line: number;
column: number;
}[];
export class ClaError extends Error {
private reasons: ClaErrorType[];
constructor(reasons: ClaErrorType[]) {
super(reasons.map(ClaError.formatClaError).join(', '));
Object.setPrototypeOf(this, ClaError.prototype);
this.reasons = reasons;
}
public static formatLineCol = (position: ClaErrorTypeLineColumn): string => {
return `line ${position.line}, column ${position.column}`;
};
public static formatClaError = (reason: ClaErrorType): string => {
const message = reason.message;
const locations = reason.locations.map(ClaError.formatLineCol).join(', ');
return `${message} on ${locations}`;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment