TypeScript Multi-Step workflow with error handling
TypeScript Error Handling : Exercise-11 with Solution
Write a TypeScript program that simulates a multi-step workflow. Create custom error classes for each step of the workflow. Implement error propagation to handle and log errors at each step.
Sample Solution:
TypeScript Code:
// Custom error classes for each step of the workflow
class Step1Error extends Error {
constructor(message: string) {
super(message);
this.name = 'Step1Error';
}
}
class Step2Error extends Error {
constructor(message: string) {
super(message);
this.name = 'Step2Error';
}
}
class Step3Error extends Error {
constructor(message: string) {
super(message);
this.name = 'Step3Error';
}
}
// Function to simulate step 1 of the workflow
function simulateStep1(): Promise {
return new Promise((resolve, reject) => {
setTimeout(() => {
// Simulate an error in step 1
reject(new Step1Error('Error in Step 1'));
}, 1000);
});
}
// Function to simulate step 2 of the workflow
function simulateStep2(): Promise {
return new Promise((resolve, reject) => {
setTimeout(() => {
// Simulate an error in step 2
reject(new Step2Error('Error in Step 2'));
}, 1000);
});
}
// Function to simulate step 3 of the workflow
function simulateStep3(): Promise {
return new Promise((resolve, reject) => {
setTimeout(() => {
// Simulate a successful step 3
resolve();
}, 1000);
});
}
// Main workflow function
async function multiStepWorkflow(): Promise {
try {
await simulateStep1();
} catch (error) {
console.error(`Step 1 Error: ${error.message}`);
throw error; // Propagate the error to the calling function
}
try {
await simulateStep2();
} catch (error) {
console.error(`Step 2 Error: ${error.message}`);
throw error; // Propagate the error to the calling function
}
try {
await simulateStep3();
console.log('Workflow completed successfully');
} catch (error) {
console.error(`Step 3 Error: ${error.message}`);
}
}
// Execute the multi-step workflow
multiStepWorkflow().catch((error) => {
console.error(`Main Application Error: ${error.message}`);
});
Explanations:
In the exercise above -
- First, define custom error classes (Step1Error, Step2Error, and Step3Error) to represent errors at each step of the workflow.
- The "simulateStep1()", "simulateStep2()", and "simulateStep3()" functions simulate the three steps of the workflow. Step 1 and Step 2 simulate errors, while Step 3 simulates a successful completion.
- The "multiStepWorkflow()" function executes the steps sequentially and handles errors for each step. If any step encounters an error, it logs it and propagates it to the calling function.
- Finally, execute the multiStepWorkflow and catch any errors that may occur during the workflow or at the application level.
Output:
Step 1 Error: Error in Step 1 Main Application Error: Error in Step 1
TypeScript Editor:
See the Pen TypeScript by w3resource (@w3resource) on CodePen.
Previous: TypeScript sequential API requests with error handling.
Next: TypeScript database transactions with error handling.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics