Skip to content

feat(util-invariant): Convert to use util-logger under the hood #2418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/util-invariant/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"start": "fcl-bundle --watch"
},
"dependencies": {
"@babel/runtime": "^7.25.7"
"@babel/runtime": "^7.25.7",
"@onflow/util-logger": "^1.3.3"
}
}
23 changes: 17 additions & 6 deletions packages/util-invariant/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import * as logger from "@onflow/util-logger"

/**
* Asserts fact is true, otherwise throw an error with invariant message
* @param fact
* @param msg
* @param rest
* Asserts fact is true, otherwise logs error and optionally throws
* @param fact - The condition to assert
* @param msg - The message to log if assertion fails
* @param loggerLevel - Optional logger level (defaults to error which will throw)
* @param rest - Additional arguments to log
*/
export function invariant(
fact: boolean,
Expand All @@ -15,7 +18,15 @@ export function invariant(
?.split("\n")
?.filter(d => !/at invariant/.test(d))
?.join("\n")
console.error("\n\n---\n\n", error, "\n\n", ...rest, "\n\n---\n\n")
throw error

logger.log({
title: "Invariant Violation",
message: `${error.message}\n${rest.length ? "\nDetails:" + JSON.stringify(rest, null, 2) : ""}`,
level: loggerLevel
})

if (loggerLevel === logger.LEVELS.error) {
throw error
}
}
}