Skip to content
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loggerLevel is not defined, this should just always be set to logger.LEVELS.error

})

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