Error Handling
In case of failures processTask method should throw PicsArtError .
throw new PicArtError('<reason>', '<message>', '<http_status_code>');
- reason: lowercased error code, for programmatic handling. should not contain spaces. i.e., not_found
- message: complete description of the error
- http_status_code: http status code which API should return when error is not caught
Make sure your error reason and message include all required info to understand the root cause of the error and handle it if needed: E.g.
throw new PicArtError(
'invalid_image_size',
`Image generation with model ${model} failed due to invalid image size: Image size should be less than 1Mb`,
400
);
Error Handling Utils
There are built-in utility methods, which make handling errors from API calls easier.
import { flowUtils } from '@picsart/pa-pluggable-workers-core';
throw flowUtils.fromApiError(
command, // command/api name used in default message/reason resolvers
err, // original api error
(response: AxiosResponse<any>) => 'message', // optional method to resolve error message from API error response
(response: AxiosResponse<any>)=> 'reason', // optional method to resolve error reason from API error response
(response: AxiosResponse<any>)=> 400, // optional method to resolve error status code from API error response
);
In addition, there are wrappers of fromApiError method designed to handle errors of Picsart native APIs
fromPicsartAiApiError- for Picsart AI apisfromPicsartCoreApiError- for Picsart Core apis
Status Code Mapping
There is a built-in method to map external API status codes to error status code
const status = mapStatusCode(400)
By default, 400, 404, 429, 422 are mapped to the same value, and anything beside them is converted to 503. You can also override this behavior by providing a custom mapping
const status = mapStatusCode(400, {
429: 503
})