@adobe/aio-lib-core-errors
Advanced tools
Comparing version 1.0.1 to 2.0.0
{ | ||
"name": "@adobe/aio-lib-core-errors", | ||
"version": "1.0.1", | ||
"description": "Adobe I/O CNA SDK Core Errors", | ||
"version": "2.0.0", | ||
"description": "Adobe I/O SDK Core Errors", | ||
"main": "src/index.js", | ||
@@ -12,3 +12,3 @@ "scripts": { | ||
"type": "git", | ||
"url": "https://git.corp.adobe.com/CNA/aio-lib-core-errors.git" | ||
"url": "https://github.com/adobe/aio-lib-core-errors.git" | ||
}, | ||
@@ -18,2 +18,3 @@ "author": "Adobe Inc.", | ||
"devDependencies": { | ||
"codecov": "^3.5.0", | ||
"eslint": "^6.2.2", | ||
@@ -20,0 +21,0 @@ "eslint-config-standard": "^14.0.1", |
129
README.md
@@ -17,9 +17,22 @@ <!-- | ||
## AioCoreSDKError | ||
This is the base class for all the Error classes, and it should not be instantiated directly. Create your own subclass (dynamic usage) or use the ErrorWrapper below (static usage). | ||
The `message` property of `AioCoreSDKError` outputs the Error in this format: | ||
```bash | ||
[SDK:ERROR_CODE] ERROR_MESSAGE | ||
``` | ||
- `SDK` is the SDK name | ||
- `ERROR_CODE` is the error code | ||
- `ERROR_MESSAGE` is the error message | ||
Note that the `sdk details` is not displayed in the message. To see any `sdk details`, you will need to convert the Error object itself to JSON via `.toJSON()` (`JSON.stringify` will convert the Error object as well, via invoking `.toJSON()` on the Error object) | ||
## Dynamic Errors Usage | ||
```javascript | ||
// Adobe I/O CNA Campaign Standard Wrapper | ||
// Adobe I/O Campaign Standard Wrapper | ||
// proposed namespace | ||
const { CNACoreSDKError } = require('@adobe/adobe-io-cna-errors') | ||
const { AioCoreSDKError } = require('@adobe/aio-lib-errors') | ||
@@ -31,6 +44,7 @@ const gSDKDetails = { | ||
} | ||
const gSDKName = "AdobeIOCNACampaignStandard" | ||
const gSDKName = "AdobeIOCampaignStandard" | ||
export default class CampaignStandardCoreAPIError extends CNACoreSDKError { | ||
export default class CampaignStandardCoreAPIError extends AioCoreSDKError { | ||
constructor(message, code) { | ||
// the sdk name and sdk details are curried in as the 3rd and 4th parameters | ||
super(message, code, gSDKName, gSDKDetails) | ||
@@ -43,3 +57,3 @@ } | ||
```javascript | ||
getAllProfiles (parameters) { | ||
function getAllProfiles (parameters) { | ||
return new Promise((resolve, reject) => { | ||
@@ -62,3 +76,3 @@ const apiFunc = 'getAllProfiles' | ||
```javascript | ||
getAllProfiles (parameters) { | ||
function getAllProfiles (parameters) { | ||
return new Promise((resolve, reject) => { | ||
@@ -73,3 +87,3 @@ const apiFunc = 'getAllProfiles' | ||
// >>>>>NEW<<<<<<<<< | ||
reject(new CampaignStandardCoreAPIError(err.message, apiFunc)) | ||
reject(new CampaignStandardCoreAPIError(err.message /* error message */, apiFunc /* error code */)) | ||
}) | ||
@@ -86,6 +100,9 @@ }) | ||
We can define our specialized Error class, and error codes in its own module like so: | ||
### 1. Create your Error Class via the ErrorWrapper | ||
We define our specialized Error class, and error codes in its own module like so: | ||
```javascript | ||
// MySDKError.js | ||
const { ErrorWrapper, createUpdater } = require('../../src').CNACoreSDKErrorWrapper | ||
// SDKErrors.js | ||
const { ErrorWrapper, createUpdater } = require('@adobe/aio-lib-core-errors').AioCoreSDKErrorWrapper | ||
@@ -115,4 +132,4 @@ const codes = {} | ||
Updater | ||
// the base class that your Error class is extending. CNACoreSDKError is the default | ||
/* CNACoreSDKError, */ | ||
// the base class that your Error class is extending. AioCoreSDKError is the default | ||
/* , AioCoreSDKError */ | ||
) | ||
@@ -130,3 +147,3 @@ | ||
## Error Class Wrapper | ||
#### Error Class Wrapper | ||
@@ -139,11 +156,11 @@ Let's parse what this line means: | ||
This will dynamically create a `MySDKError` class with the appropriate closures for values: | ||
- sdk name | ||
- sdk error class name | ||
- error code | ||
- error message | ||
- sdk name (`MySDK`, passed in to the ErrorWrapper constructor above) | ||
- sdk error class name (`MySDKError`, passed in to the ErrorWrapper constructor above) | ||
- error code (`UNKNOWN_ORDER_ID`, the first parameter passed in to `E`) | ||
- error message (`There was a problem with that order id: %s.`, the second parameter passed in to `E`. If this is a string with `format specifiers`, you can pass in arguments for the format specifiers, when the Error is constructed. See example below). | ||
```javascript | ||
class MySDKError extends CNACoreSDKError { ... } | ||
class MySDKError extends AioCoreSDKError { ... } | ||
``` | ||
The line will add the dynamically created `MySDKError` class to the `codes` object, with the first parameter to the wrapper (the error code) as the key. | ||
The line will add the dynamically created `MySDKError` class to the exported `codes` object, with the first parameter to the wrapper (the error code) as the key. | ||
@@ -155,4 +172,4 @@ The `constructor` for the class created takes one parameter, an object: | ||
- `parameters` (optional) is an `object` | ||
- `parameters.sdkDetails` (optional) is an `object` that you want to pass in as additional data for the Error object. | ||
- `parameters.messageValues` (optional) is a `string`, `number`, `object` or an `array` of the items that you want to apply to the error message, if the message has a [format specifier](https://nodejs.org/api/util.html#util_util_format_format_args) | ||
- `parameters.sdkDetails` (optional) is an `object` that you want to pass in as additional data for the Error object. e.g. function parameters for an API call. | ||
- `parameters.messageValues` (optional) is a `string`, `number`, `object` or an `array` of the items that you want to apply to the error message, if the message has a [format specifier](https://nodejs.org/api/util.html#util_util_format_format_args). | ||
@@ -164,7 +181,9 @@ In the error specification line below, you can see it has one format specifier `%s`: | ||
## Static Errors Usage | ||
### 2. Use Your Error Classes | ||
Import the static error code Error classes, and use directly. | ||
```javascript | ||
const { UNKNOWN_THING_ID, UNKNOWN_ORDER_ID } = require('./MySDKError').codes | ||
const { UNKNOWN_THING_ID, UNKNOWN_ORDER_ID } = require('./SDKErrors').codes | ||
// or, if you have too many codes to destructure: | ||
// const { codes } = require('./SDKErrors') | ||
@@ -182,3 +201,3 @@ const gSdkDetails = { | ||
if (unknownOrderId) { | ||
// messageValues can either be a string or array of strings | ||
// messageValues can either be a string, object, etc or an array of them | ||
throw new UNKNOWN_ORDER_ID({ | ||
@@ -189,16 +208,40 @@ sdkDetails: gSDKDetails, | ||
} | ||
function getOrder(orderId) { | ||
// we just wrap the function arguments as the sdkDetails | ||
const sdkDetails = { orderId } | ||
const somethingWentWrong = true | ||
if (somethingWentWrong) { | ||
// messageValues can either be a string, object, etc or an array of them | ||
throw new UNKNOWN_ORDER_ID({ | ||
sdkDetails: sDKDetails, | ||
messageValues: 'ORDER-2125SFGF' | ||
}) ) | ||
} | ||
} | ||
getOrder('abc123') | ||
``` | ||
## Example Console Output | ||
Print out a thrown `UNKNOWN_ORDER_ID` Error object to console: | ||
--- | ||
Print out a thrown `UNKNOWN_ORDER_ID` Error object to the console. `console` calls the `.toString()` method for the Error object, and the resulting data is not structured well, and will not expand nested objects: | ||
```javascript | ||
try { | ||
throw new UNKNOWN_ORDER_ID() | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
``` | ||
Output: | ||
```bash | ||
{ MySDKError: [MySDK:UNKNOWN_ORDER_ID] There was a problem with that order id: ORDER-21241-FSFS. | ||
at new <anonymous> (/Users/obfuscated/adobeio-cna-errors/src/CNACoreSDKErrorWrapper.js:22:9) | ||
at Object.<anonymous>.test (/Users/obfuscated/adobeio-cna-errors/test/MySDKError.test.js:50:15) | ||
at Object.asyncJestTest (/Users/obfuscated/adobeio-cna-errors/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:102:37) | ||
at resolve (/Users/obfuscated/adobeio-cna-errors/node_modules/jest-jasmine2/build/queueRunner.js:43:12) | ||
at new <anonymous> (/Users/obfuscated/aio-lib-core-errors/src/AioCoreSDKErrorWrapper.js:22:9) | ||
at Object.<anonymous>.test (/Users/obfuscated/aio-lib-core-errors/test/MySDKError.test.js:50:15) | ||
at Object.asyncJestTest (/Users/obfuscated/aio-lib-core-errors/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:102:37) | ||
at resolve (/Users/obfuscated/aio-lib-core-errors/node_modules/jest-jasmine2/build/queueRunner.js:43:12) | ||
at new Promise (<anonymous>) | ||
at mapper (/Users/obfuscated/adobeio-cna-errors/node_modules/jest-jasmine2/build/queueRunner.js:26:19) | ||
at promise.then (/Users/obfuscated/adobeio-cna-errors/node_modules/jest-jasmine2/build/queueRunner.js:73:41) | ||
at mapper (/Users/obfuscated/aio-lib-core-errors/node_modules/jest-jasmine2/build/queueRunner.js:26:19) | ||
at promise.then (/Users/obfuscated/aio-lib-core-errors/node_modules/jest-jasmine2/build/queueRunner.js:73:41) | ||
at process._tickCallback (internal/process/next_tick.js:68:7) | ||
@@ -210,9 +253,17 @@ code: 'UNKNOWN_ORDER_ID', | ||
``` | ||
Print out a thrown `UNKNOWN_ORDER_ID` Error object's `message` to console: | ||
--- | ||
Simply print out a thrown `UNKNOWN_ORDER_ID` Error object's `message` property to the console: | ||
```javascript | ||
try { | ||
throw new UNKNOWN_ORDER_ID() | ||
} catch (e) { | ||
console.error(e.message) | ||
} | ||
``` | ||
Output: | ||
```bash | ||
[MySDK:UNKNOWN_ORDER_ID] There was a problem with that order id: ORDER-21241-FSFS. | ||
``` | ||
Convert a thrown `UNKNOWN_ORDER_ID` Error object to JSON and print to console: | ||
--- | ||
Convert a thrown `UNKNOWN_ORDER_ID` Error object to JSON and print to the console. `console` calls the `.toJSON()` method of the Error object, and will expand nested objects: | ||
```javascript | ||
@@ -222,3 +273,3 @@ try { | ||
} catch (e) { | ||
console.log(JSON.stringify(e, null, 2)) | ||
console.error(JSON.stringify(e, null, 2)) | ||
} | ||
@@ -237,4 +288,4 @@ ``` | ||
"message": "[MySDK:UNKNOWN_ORDER_ID] There was a problem with that order id: ORDER-21241-FSFS.", | ||
"stacktrace": "MySDKError: [MySDK:UNKNOWN_ORDER_ID] There was a problem with that order id: ORDER-21241-FSFS.\n at new <anonymous> (/Users/obfuscated/adobeio-cna-errors/src/CNACoreSDKErrorWrapper.js:22:9)\n at Object.<anonymous>.test (/Users/obfuscated/adobeio-cna-errors/test/MySDKError.test.js:50:15)\n at Object.asyncJestTest (/Users/obfuscated/adobeio-cna-errors/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:102:37)\n at resolve (/Users/obfuscated/adobeio-cna-errors/node_modules/jest-jasmine2/build/queueRunner.js:43:12)\n at new Promise (<anonymous>)\n at mapper (/Users/obfuscated/adobeio-cna-errors/node_modules/jest-jasmine2/build/queueRunner.js:26:19)\n at promise.then (/Users/obfuscated/adobeio-cna-errors/node_modules/jest-jasmine2/build/queueRunner.js:73:41)\n at process._tickCallback (internal/process/next_tick.js:68:7)" | ||
"stacktrace": "MySDKError: [MySDK:UNKNOWN_ORDER_ID] There was a problem with that order id: ORDER-21241-FSFS.\n at new <anonymous> (/Users/obfuscated/aio-lib-core-errors/src/AioCoreSDKErrorWrapper.js:22:9)\n at Object.<anonymous>.test (/Users/obfuscated/aio-lib-core-errors/test/MySDKError.test.js:50:15)\n at Object.asyncJestTest (/Users/obfuscated/aio-lib-core-errors/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:102:37)\n at resolve (/Users/obfuscated/aio-lib-core-errors/node_modules/jest-jasmine2/build/queueRunner.js:43:12)\n at new Promise (<anonymous>)\n at mapper (/Users/obfuscated/aio-lib-core-errors/node_modules/jest-jasmine2/build/queueRunner.js:26:19)\n at promise.then (/Users/obfuscated/aio-lib-core-errors/node_modules/jest-jasmine2/build/queueRunner.js:73:41)\n at process._tickCallback (internal/process/next_tick.js:68:7)" | ||
} | ||
``` |
@@ -12,8 +12,8 @@ /* | ||
const CNACoreSDKError = require('./CNACoreSDKError') | ||
const CNACoreSDKErrorWrapper = require('./CNACoreSDKErrorWrapper') | ||
const AioCoreSDKError = require('./AioCoreSDKError') | ||
const AioCoreSDKErrorWrapper = require('./AioCoreSDKErrorWrapper') | ||
module.exports = { | ||
CNACoreSDKError, | ||
CNACoreSDKErrorWrapper | ||
AioCoreSDKError, | ||
AioCoreSDKErrorWrapper | ||
} |
@@ -12,3 +12,3 @@ /* | ||
const { ErrorWrapper, createUpdater } = require('../../src').CNACoreSDKErrorWrapper | ||
const { ErrorWrapper, createUpdater } = require('../../src').AioCoreSDKErrorWrapper | ||
@@ -38,4 +38,4 @@ const codes = {} | ||
Updater | ||
// the base class that your Error class is extending. CNACoreSDKError is the default | ||
/* CNACoreSDKError, */ | ||
// the base class that your Error class is extending. AioCoreSDKError is the default | ||
/* AioCoreSDKError, */ | ||
) | ||
@@ -42,0 +42,0 @@ |
@@ -16,3 +16,3 @@ /* | ||
const { UNKNOWN_THING_ID, UNKNOWN_ORDER_ID } = require('./classes/MySDKError').codes | ||
const { CNACoreSDKError } = require('../src') | ||
const { AioCoreSDKError } = require('../src') | ||
@@ -35,3 +35,3 @@ test('codes', () => { | ||
expect(err instanceof CNACoreSDKError).toBeTruthy() | ||
expect(err instanceof AioCoreSDKError).toBeTruthy() | ||
expect(err.sdk).toEqual(sdk) | ||
@@ -38,0 +38,0 @@ expect(err.sdkDetails).toEqual(sdkDetails) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39726
13
276
9
1