forked from tact-lang/tact
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: enhance error handling to support third-party tools (tact-lang#509)
- Replaced consoleLogger with the new Logger - Removed jest mock where consoleLogger was used - Add `TactErrorCollection` type - Add `-q`/`--quiet` flags to suppress compiler log output Co-authored-by: Novus Nota <[email protected]> Co-authored-by: byakuren-hijiri <[email protected]> Co-authored-by: Anton Trunov <[email protected]>
- Loading branch information
1 parent
d8893ca
commit 1d6dd06
Showing
17 changed files
with
280 additions
and
214 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,87 @@ | ||
export type TactLogger = { | ||
log: (message: string) => void; | ||
error: (message: string) => void; | ||
}; | ||
export enum LogLevel { | ||
/** Logging is turned off */ | ||
NONE, | ||
/** Logs only error messages */ | ||
ERROR, | ||
/** Logs warning and error messages */ | ||
WARN, | ||
/** Logs informational, warning, and error messages */ | ||
INFO, | ||
/** Logs debugging, informational, warning, and error messages */ | ||
DEBUG, | ||
} | ||
|
||
type messageType = string | Error; | ||
|
||
export const consoleLogger: TactLogger = { | ||
log: console.log, | ||
error: console.error, | ||
export interface LogMethods { | ||
debug: (message: messageType) => void; | ||
info: (message: messageType) => void; | ||
warn: (message: messageType) => void; | ||
error: (message: messageType) => void; | ||
} | ||
|
||
const logLevelToMethodName: { [key in LogLevel]: keyof LogMethods | null } = { | ||
[LogLevel.NONE]: null, | ||
[LogLevel.ERROR]: "error", | ||
[LogLevel.WARN]: "warn", | ||
[LogLevel.INFO]: "info", | ||
[LogLevel.DEBUG]: "debug", | ||
}; | ||
|
||
function getLoggingMethod(level: LogLevel): keyof LogMethods | null { | ||
return logLevelToMethodName[level]; | ||
} | ||
|
||
export class Logger { | ||
private level: LogLevel; | ||
private logMethods: LogMethods; | ||
|
||
constructor(level: LogLevel = LogLevel.INFO) { | ||
this.level = level; | ||
this.logMethods = { | ||
debug: console.log, | ||
info: console.log, | ||
warn: console.warn, | ||
error: console.error, | ||
}; | ||
} | ||
|
||
protected log(level: LogLevel, message: messageType) { | ||
if (this.level === LogLevel.NONE) { | ||
return; | ||
} | ||
|
||
if (message instanceof Error) { | ||
message = message.stack ?? message.message; | ||
} else { | ||
message = message.toString(); | ||
} | ||
|
||
if (level > this.level) return; | ||
|
||
const loggingMethod = getLoggingMethod(level); | ||
if (!loggingMethod) return; | ||
|
||
this.logMethods[loggingMethod](message); | ||
} | ||
|
||
debug(message: messageType) { | ||
this.log(LogLevel.DEBUG, message); | ||
} | ||
|
||
info(message: messageType) { | ||
this.log(LogLevel.INFO, message); | ||
} | ||
|
||
warn(message: messageType) { | ||
this.log(LogLevel.WARN, message); | ||
} | ||
|
||
error(message: messageType) { | ||
this.log(LogLevel.ERROR, message); | ||
} | ||
|
||
setLevel(level: LogLevel) { | ||
this.level = level; | ||
} | ||
} |
Oops, something went wrong.