Skip to content

Commit

Permalink
fix error and transpiler
Browse files Browse the repository at this point in the history
  • Loading branch information
USERSATOSHI committed Jan 14, 2025
1 parent 5d6b88e commit 26c5aea
Show file tree
Hide file tree
Showing 2 changed files with 281 additions and 132 deletions.
243 changes: 176 additions & 67 deletions lib/aoi.js/src/core/error.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,183 @@
import { type ICodeFunctionData } from '@aoi.js/typings/interface.js';

export class TranspilerError extends Error {

static CompileError(msg: string, data: ICodeFunctionData) {
return new TranspilerError(`CompileError: ${msg}`, {
function: {
name: data.name,
code: data.inside ?? '',
},
cmd: data.cmd?.name,
path: data.cmd?.__path__,
code: data.cmd?.code.toString(),
});
}
// import { type ICodeFunctionData } from '@aoi.js/typings/interface.js';

static RunTimeError(msg: string, data: ICodeFunctionData) {
return new TranspilerError(`RunTimeError: ${msg}`, {
function: {
name: data.name,
code: data.inside ?? '',
},
cmd: data.cmd?.name,
path: data.cmd?.__path__,
code: data.cmd?.code.toString(),
});
}
// export class TranspilerError extends Error {

static AoiReaderError(msg: string, code: string) {
return new TranspilerError(`AoiReaderError: ${msg}`, {
code,
});
}
// static CompileError(msg: string, data: ICodeFunctionData) {
// return new TranspilerError(`CompileError: ${msg}`, {
// function: {
// name: data.name,
// code: data.inside ?? '',
// },
// cmd: data.cmd?.name,
// path: data.cmd?.__path__,
// code: data.cmd?.code.toString(),
// });
// }

// static RunTimeError(msg: string, data: ICodeFunctionData) {
// return new TranspilerError(`RunTimeError: ${msg}`, {
// function: {
// name: data.name,
// code: data.inside ?? '',
// },
// cmd: data.cmd?.name,
// path: data.cmd?.__path__,
// code: data.cmd?.code.toString(),
// });
// }

// static AoiReaderError(msg: string, code: string) {
// return new TranspilerError(`AoiReaderError: ${msg}`, {
// code,
// });
// }

// function?: { name: string; code: string };
// cmd?: string;
// path?: string;
// code: string | undefined;
// constructor(
// msg: string,
// data?: {
// function?: {
// name: string;
// code: string;
// };
// cmd?: string;
// path?: string;
// code?: string;
// },
// ) {
// super(msg);
// this.name = 'TranspilerError';
// // this.cause = this;
// this.function = data?.function;
// this.cmd = data?.cmd;
// this.path = data?.path;
// this.code = data?.code;
// }

// toString() {
// return `[TranspilerError]|> ${this.message} {
// ${
// this.function
// ? `function: {
// name: ${this.function.name}
// code: ${this.function.code.trim()}
// }`
// : ''
// }${this.cmd ? `cmd: ${this.cmd}` : ''}${
// this.path ? `path: ${this.path}` : ''
// }${this.code ? `code: ${this.code}` : ''}
// }`;
// }
// }

import type Command from '@aoi.js/classes/Command.js';
import type Macro from '@aoi.js/classes/Macro.js';
import { AoiErrorType, ErrorCode } from '@aoi.js/typings/enum.js';
import type {
ICodeFunctionData,
ICommandOptions,
IMacroOptions,
} from '@aoi.js/typings/interface.js';

function?: { name: string; code: string };
export default class AoijsErrorHandler extends Error {
static FunctionError = (
errorCode: ErrorCode,
message: string,
data: ICodeFunctionData,
) => {
const err = new AoijsErrorHandler(
AoiErrorType.FunctionError,
errorCode,
message,
);
err.function = data.name;
err.cmd = data.cmd?.name;
err.code = data.cmd?.code.toString();
return err;
};

static CompilerError = (
errorCode: ErrorCode,
message: string,
cmd?: Command,
transpiledCode?: string,
) => {
const err = new AoijsErrorHandler(
AoiErrorType.CompilerError,
errorCode,
message,
);
err.cmd = cmd?.name;
err.code = cmd?.code.toString();
if (transpiledCode) err.code = transpiledCode;
return err;
};

static ReaderError = (
errorCode: ErrorCode,
message: string,
code: string,
) => {
const err = new AoijsErrorHandler(
AoiErrorType.ReaderError,
errorCode,
message,
);
err.code = code;
return err;
};

static CommandError = (
errorCode: ErrorCode,
message: string,
cmd: Command | ICommandOptions,
) => {
const err = new AoijsErrorHandler(
AoiErrorType.CommandError,
errorCode,
message,
);
err.cmd = cmd.name;
err.code = cmd.code.toString();
return err;
};

static MacroError = (
errorCode: ErrorCode,
message: string,
macro: Macro | IMacroOptions,
) => {
const err = new AoijsErrorHandler(
AoiErrorType.MacroError,
errorCode,
message,
);
err.cmd = macro.name;
err.code = macro.code.toString();
return err;
};

errorCode: keyof typeof ErrorCode;
function?: string;
code?: string;
cmd?: string;
path?: string;
code: string | undefined;
constructor(
msg: string,
data?: {
function?: {
name: string;
code: string;
};
cmd?: string;
path?: string;
code?: string;
},
) {
super(msg);
this.name = 'TranspilerError';
// this.cause = this;
this.function = data?.function;
this.cmd = data?.cmd;
this.path = data?.path;
this.code = data?.code;
constructor(type: AoiErrorType, errorCode: ErrorCode, message: string) {
super(message);
Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain
this.errorCode = this.#getErrorCode(errorCode);
this.name = this.#getErrorType(type);

Error.captureStackTrace(this, this.constructor); // keep stack trace clean
}

#getErrorType(type: AoiErrorType) {
return AoiErrorType[type];
}

toString() {
return `[TranspilerError]|> ${this.message} {
${
this.function
? `function: {
name: ${this.function.name}
code: ${this.function.code.trim()}
}`
: ''
}${this.cmd ? `cmd: ${this.cmd}` : ''}${
this.path ? `path: ${this.path}` : ''
}${this.code ? `code: ${this.code}` : ''}
}`;
#getErrorCode(code: ErrorCode) {
return ErrorCode[code] as keyof typeof ErrorCode;
}
}
}
Loading

0 comments on commit 26c5aea

Please sign in to comment.