This code provides utility functions to handle and log errors with Rollbar integration in a Node.js environment, including rescheduling tasks and triggering Rollbar error logging based on certain conditions.
npm install @gatekeeper_technology/rollbar-utilstypescript
import RollbarUtil from "@gatekeeper_technology/rollbar-utils";let rollbar: RollbarUtil;
export async function run(this: TaskContext, params: any) {
console.log(
🏁 Task started (in ${CloudCode.task.env}) with params: ${JSON.stringify(params ?? (params = {}), null, 2)});
rollbar = new RollbarUtil({
access_token: process.env.ROLLBAR_ACCESS_TOKEN,
params: params,
self: this
}); try {
await runTask.call(this, request_object);
} catch (error) {
await rollbar.handleError(error);
}
}
`
$3
* When working with
.js files, you can import the package as shown below:
`javascript
const { default: RollbarUtil } = require("@gatekeeper_technology/rollbar-utils");
`
$3
* No need to import Rollbar
* Manually update your dependency
`json
"dependencies": {
"@gatekeeper_technology/rollbar-utils": "",
"@journeyapps/cloudcode": "1.12.0"
},
` ###
tsconfig.json (only for TypeScript):
* Replace your tsconfig.json file with:
`json
{
"extends": "@journeyapps/cloudcode-build/task-tsconfig",
"compilerOptions": {
"esModuleInterop": true,
"allowJs": false,
"skipLibCheck": true
}
}
`
* Don't forget to register your Rollbar access token using deployment environment variable ROLLBAR_ACCESS_TOKEN.
For testing, you can use the token that is pinned in the #rollbar-testing-spam Slack channel* Note: If you get an linting error of copying and pasting, try refreshing OXIDE.
Detailed Usage
$3
`javascript
await rollbar.handleError(error);
`Or with options:
`javascript
await rollbar.handleError(error, { reschedule_task: false });
`#### Flags
-
env: Specifies environment(s) to log or reschedule errors. Default is "production".
- Can be string or string[]
- reschedule_task: Auto-reschedule task on failure. Default is true.
- trigger_rollbar: Log errors to Rollbar. Default is true.$3
The throwError function allows you to throw custom errors with configurable settings. You can control whether to trigger Rollbar logging or to reschedule the task. By default, the settings are { trigger_rollbar: true, reschedule_task: false }.Here's how to use
throwError:`javascript
rollbar.throwError("Will not trigger rollbar", { trigger_rollbar: false });
rollbar.throwError("Will trigger rollbar, but not reschedule", { trigger_rollbar: true, reschedule_task: false });
rollbar.throwError("Will not trigger rollbar, and not reschedule", { trigger_rollbar: false, reschedule_task: false });
`Note: All actions are implemented only in the specified environments, which defaults to
"production."Custom Error Handling
Our utility provides fine-grained control over how certain types of errors are managed. This section explains how to work with two specific custom errors:
status-not-pending and attachments-uploading.$3
This type of error is considered a "blocking" error. If such an error occurs, the task will not be retried. $3
For this error type, Rollbar logging is conditional based on the retry count. The utility will only log a Rollbar error on the final retry.Changelog
$3
#### Updated
- Dependencies to latest versions$3
#### Updated
- Dependencies to latest versions$3
#### Updated
- Dependencies to latest versions$3
#### Added
- Function to add object ids to the CC parameters for objects in the CC parameters. $3
#### Added
- Introduced throwError function for more granular error handling.
- Allows custom errors that can be set to either retry or not.
- Can also be set to log or not log in Rollbar.#### Changed
- Refactored repository structure from a single file to a multi-file architecture.
- Improves modularity and maintainability of the code.
- Improved the README
$3
#### Changed
- Changed the Rollbar Config Validation to only log the errors in the console and not throw the Errors.
- If the env is not set, it defaults to 'testing'
- If the trace_id is not set, it defaults to 'not_applicable'$3
#### Added
- Enhanced error analysis: The package now checks the error's name, message, and cause to determine whether the error should be ignored.#### Changed
- Updated
README.md with both concise and comprehensive usage instructions.
- Renamed the env_to_error flag to env.
- The env flag now specifies the environments for which the CC Task may trigger rollbar and reschedule.
- All console.warn or console.error are changed to console.log, due to not always logging the messages.$3
#### Added
- 'status-not-pending' errors no longer rescheduling task or log on rollbar.
#### Changed
- Converted functions to a Class
- Improve console logging.
- Better Exception handling#### Fixed
- Typo
- Edge case where if a CC Task did not reschedule, rollbar would retrigger even if asked not to.
$3
#### Fixed
- Reverted hook function change in version 1.0.7.$3
#### Fixed
- A bug where the context could not be found due to the hook function being async.$3
#### Changed
- Changed functions to hook functions.
#### Fixed
- A bug where the error names were not read correctly.
- A bug where a null/undefined` retry_count stopped the task from retrying.#### Fixed
- a bug that attempted to log task even without an rollbar access token
#### Fixed
- A bug where the 'shouldReschedule' flag was ignored.
#### Removed
- Removed TS's type checking on 'this'.
#### Changed
- Implemented 'trigger_rollbar' flag.
- Improved on console logs.
#### Fixed
- Typo fixed.
#### Fixed
- Resolved ToDos