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.
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);
}
}
- When working with
.js
files, you can import the package as shown below:const { default: RollbarUtil } = require("@gatekeeper_technology/rollbar-utils");
- No need to import Rollbar
- Manually update your dependency
"dependencies": { "@gatekeeper_technology/rollbar-utils": "<latest_version>", "@journeyapps/cloudcode": "1.12.0" },
- Replace your
tsconfig.json
file with:
{
"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.
await rollbar.handleError(error);
Or with options:
await rollbar.handleError(error, { reschedule_task: false });
-
env
: Specifies environment(s) to log or reschedule errors. Default is"production"
.- Can be
string
orstring[]
- Can be
-
reschedule_task
: Auto-reschedule task on failure. Default istrue
. -
trigger_rollbar
: Log errors to Rollbar. Default istrue
.
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
:
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."
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
.
This type of error is considered a "blocking" error. If such an error occurs, the task will not be retried.
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.
- Dependencies to latest versions
- Dependencies to latest versions
- Dependencies to latest versions
- Function to add object ids to the CC parameters for objects in the CC parameters.
- 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.
- Refactored repository structure from a single file to a multi-file architecture.
- Improves modularity and maintainability of the code.
- Improved the README
- 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'
- Enhanced error analysis: The package now checks the error's name, message, and cause to determine whether the error should be ignored.
- Updated
README.md
with both concise and comprehensive usage instructions. - Renamed the
env_to_error
flag toenv
. - 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.
- 'status-not-pending' errors no longer rescheduling task or log on rollbar.
- Converted functions to a Class
- Improve console logging.
- Better Exception handling
- Typo
- Edge case where if a CC Task did not reschedule, rollbar would retrigger even if asked not to.
- Reverted hook function change in version 1.0.7.
- A bug where the context could not be found due to the hook function being async.
- Changed functions to hook functions.
- A bug where the error names were not read correctly.
- A bug where a
null
/undefined
retry_count stopped the task from retrying.
- More improved console logs!
- a bug that attempted to log task even without an rollbar access token
- Added comments on the two exported functions so that it helps developers.
- Error handling for inside the package.
- A bug where the 'shouldReschedule' flag was ignored.
- Added optional 'shouldReschedule' flag.
- Removed TS's type checking on 'this'.
- Added 'attachments-uploading' check.
- Implemented 'trigger_rollbar' flag.
- Improved on console logs.
- Typo fixed.
- Added .d.ts files.
- Resolved ToDos
- Made the package public
- Initial functionality that logs errors through rollbar, if it does not reschedule itself.