Debugging
Custom functions can be debugged in production and when developing locally. There are many ways to debug code, and specific integrated development environments (IDEs) that provide special tooling. This page addresses general ways to debug custom functions in 8base.
Production Debugging
When a custom function gets deployed to a workspace, it becomes visible in the Function Dashboard in the 8base console. This confirms that the function was successfully deployed and is where the custom functions execution logs can get reviewed. Logs report each request uniquely.
START RequestId: 250be4db-cac4-425a-9d4c-24f94ccefd7f Version: $LATEST
END RequestId: 250be4db-cac4-425a-9d4c-24f94ccefd7f
REPORT RequestId: 250be4db-cac4-425a-9d4c-24f94ccefd7f Duration: 0.54 ms Billed Duration: 100 ms Memory Size: 1536 MB Max Memory Used: 88 MB XRAY TraceId: 1-5d794811-0a64a1f6f0c03eb43a0df3b0 SegmentId: 3353cda75bd91f53 Sampled: false
Using the console.log
and console.error
methods inside of a custom function will output any given argument to the execution logs. Knowing this, a developer has the flexibility to enrich their logs as needed, as well as output arguments for debugging purposes.
export default async (event: any, ctx: any) : Promise<LocalizeResult> => {
/* To inspecting the event.data object in production */
console.log('DEBUG: ', event.data);
// more code
Local Debugging
If you are debugging custom functions locally, we recommend using a Chrome browser. Once installed, add this script to the project’s package.json file:
"scripts": {
"debug": "node --inspect $(npm root -g)/8base-cli/dist/index.js "
}
The script runs node --inspect
, and then finds the path to the 8base-cli
package installed globally on the machine - building a path to the executable. For example: /usr/local/lib/node_modules/8base-cli/dist/index.js
. Your can use either yarn
or npm
.
Next, place a debugger
in the code where you want to pause the execution. When debugging an error, ensure that the debugger
is placed before the error occurs.
export default async (event: any, ctx: any) : Promise<LocalizeResult> => {
// some code
debugger;
// some error.
new DontExist({ something: 'is wrong' });
Before invoking the custom function, open Chrome and visit chrome://inspect
. Click the Open dedicated DevTools for Node link. The window that opens is the Chrome developer console where your debugger will be stopped.
You can now start debugging the function using the invoke-local
command.
npm run debug invoke-local FUNCTION_NAME -p /path/to/mock.json
# or
yarn debug invoke-local FUNCTION_NAME -p /path/to/mock.json
When invoke-local
is called, the 8base CLI first builds the code before running it. Therefore, it's always the built code that is being debugged. This is why the source script being debugged, when shown in the Chrome window, might look unfamiliar.
Imported modules and function arguments might have been assigned abstracted names. This does not affect any access to scope for the debugger
. If you want to inspect a module, it is better to assign it to a variable so that the name will be the same. For example;
import AWS from 'aws-sdk';
export default async (event: any, ctx: any) : Promise<LocalizeResult> => {
// Want to inspect the AWS module?
const aws = AWS;
// Now in the debugger you can be sure aws variable is available.
debugger;
// more code