IError.js (2376B)
1 import { FrameStack } from "../reducer/frameStack.js"; 2 import { ErrorMessage, REJavaScriptExn, REOther } from "./messages.js"; 3 export class IRuntimeError extends Error { 4 constructor(m, frameStack) { 5 super(); 6 this.m = m; 7 this.frameStack = frameStack; 8 } 9 static fromMessage(message) { 10 return new IRuntimeError(message, FrameStack.make()); 11 } 12 static fromMessageWithFrameStack(message, frameStack) { 13 return new IRuntimeError(message, frameStack); 14 } 15 static fromException(exn) { 16 if (exn instanceof IRuntimeError) { 17 return exn; 18 } 19 else if (exn instanceof ErrorMessage) { 20 return IRuntimeError.fromMessage(exn); 21 } 22 else if (exn instanceof Error) { 23 return IRuntimeError.fromMessage(new REJavaScriptExn(exn.message, exn.name)); 24 } 25 else { 26 return IRuntimeError.fromMessage(new REOther("Unknown exception")); 27 } 28 } 29 toString() { 30 return this.m.toString(); 31 } 32 toStringWithDetails() { 33 return (this.toString() + 34 (this.frameStack.isEmpty() 35 ? "" 36 : "\nStack trace:\n" + this.frameStack.toString())); 37 } 38 getTopFrame() { 39 return this.frameStack.getTopFrame(); 40 } 41 getFrameArray() { 42 return this.frameStack.toFrameArray(); 43 } 44 } 45 export function rethrowWithFrameStack(err, frameStack) { 46 if (err instanceof IRuntimeError) { 47 throw err; 48 } 49 else if (err instanceof ErrorMessage) { 50 throw IRuntimeError.fromMessageWithFrameStack(err, frameStack); 51 } 52 else if (err instanceof Error) { 53 throw IRuntimeError.fromMessageWithFrameStack(new REJavaScriptExn(err.message, err.name), frameStack); 54 } 55 else { 56 throw IRuntimeError.fromMessageWithFrameStack(new REOther("Unknown exception"), frameStack); 57 } 58 } 59 export class ICompileError extends Error { 60 constructor(message, location) { 61 super(); 62 this.message = message; 63 this.location = location; 64 } 65 toString() { 66 return this.message; 67 } 68 toStringWithDetails() { 69 return (this.toString() + 70 "\nLocation:\n " + 71 `at line ${this.location.start.line}, column ${this.location.start.column}, file ${this.location.source}`); 72 } 73 } 74 //# sourceMappingURL=IError.js.map