Context, Railway Error and Task
Data Context is an object injected into logic functions at runtime, which containes some key components:
Availability
- ✓ Generic logic
- ✓ Aggregator logic
Context
Type:
GenericContext
(generic logic)AggregatorContext
(aggregator logic)
When a logic gets executed, a data context object ctx
will be available in both run
and handleError
functions:
Member | Type | Description |
---|---|---|
task | Task | Return task object. See Task |
async payload() | Promise<Payload> | Return trigger metadata and payload. See Payload |
- JavaScript
- TypeScript
export async function run(ctx) {
// ctx is the data context
}
export async function handleError(ctx, error) {
// the same context as well as error
}
import { GenericContext, RailwayError } from "@fstnetwork/loc-logic-sdk";
export async function run(ctx: GenericContext) {
// ctx is the data context
}
export async function handleError(ctx: GenericContext, error: RailwayError) {
// ctx and railway error for error handling
}
Railway Error
Type:
RailwayError
RailwayError
is a object extended from the JavaScript Error
class, which will be passed to handleError()
upon an error occurred:
export async function handleError(ctx, error) {
// ...
const errorMessage = error.message; // error message
const errorLogicId = error.logicPermanentIdentity; // the PID of the logic that have error occurred
}
Member | Type | Description |
---|---|---|
name | string | Error name |
message | string | Error message |
stack | string | Error stack trace |
logicPermanentIdentity | string | Logic PID where the error originated |
logicRevision | number | Revision of the logic where the error originated |
See Tips on Error Handling for details on logic error handling.
If an error was thrown from LOC runtime (for example, from one of the agents), the stack trace would come from LOC core instead of the logic code.
Task
Type:
Task
export async function run(ctx) {
const task = ctx.task;
}
A task is an execution of a data process invoked by a trigger. The task
object is lazy-loaded and includes the following metadata:
Member | Type | Description |
---|---|---|
taskKey | TaskKey , which is { taskId: string, executionId: string } | Task ID and execution ID |
startTimestamp | Date | Task start datetime |
dataProcess | VersionedIdentityContext | Data process permanent ID |
currentLogic? | VersionedIdentityContext | Current logic permanent ID |
executedLogics | Array<VersionedIdentityContext> | An array of identity of executed logic |
Versioned Identity Context
Represents the identity of a logic or a data process:
Member | Type | Description |
---|---|---|
name | string | Name |
permanentIdentity | string | Permanent identity string (PID) |
revision | number | Revision number |
Example
const taskId = ctx.task.taskKey.taskId;
const executionId = ctx.task.taskKey.executionId;
const dpPid = ctx.task.dataProcess.permanentIdentity;
const logicPid = ctx.task.currentLogic.permanentIdentity;