Third-party Service Integration Error
Compose a ThirdPartyIntegrationError class to encapsulate errors that occur when integrating or communicating with external services.
class ThirdPartyIntegrationError extends Error {
constructor(message, service, statusCode) {
super(message);
this.name = 'ThirdPartyIntegrationError';
this.service = service;
this.statusCode = statusCode;
}
}
Defines a custom error class 'ThirdPartyIntegrationError' that extends JavaScript's native Error. It includes additional properties to specify the service and status code associated with the integration error.
function handleServiceResponse(error) {
if (error instanceof ThirdPartyIntegrationError) {
console.error(`Error with service ${error.service}: ${error.message} (Status Code: ${error.statusCode})`);
} else {
console.error('Generic error:', error);
}
}
An example function handleServiceResponse which checks if an error is an instance of ThirdPartyIntegrationError, and logs a service-specific error message. Otherwise, it logs a generic error message.
try {
// Simulated external service call that fails
throw new ThirdPartyIntegrationError('Failed to connect', 'ExampleService', 503);
} catch (error) {
handleServiceResponse(error);
}
A try...catch block where a simulated failure of an external service call throws a ThirdPartyIntegrationError. The catch block calls handleServiceResponse to handle the error appropriately.