Skip to content

Commit b84c059

Browse files
authored
chore: use '%o' to format errors in ErrorWrapper (#59)
* use %o to format errors in ErrorWrapper * fix: linter issues
1 parent 260b204 commit b84c059

3 files changed

Lines changed: 96 additions & 17 deletions

File tree

src/SDKErrors.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ module.exports = {
4545

4646
// Define your error codes with the wrapper
4747
E('ERROR_SDK_INITIALIZATION', 'SDK initialization error(s). Missing arguments: %s')
48-
E('ERROR_GET_CALCULATED_METRICS', '%s')
49-
E('ERROR_GET_CALCULATED_METRIC_BY_ID', '%s')
50-
E('ERROR_GET_COLLECTIONS', '%s')
51-
E('ERROR_GET_COLLECTION_BY_ID', '%s')
52-
E('ERROR_GET_DATE_RANGES', '%s')
53-
E('ERROR_GET_DATE_RANGE_BY_ID', '%s')
54-
E('ERROR_GET_DIMENSIONS', '%s')
55-
E('ERROR_GET_DIMENSION_BY_ID', '%s')
56-
E('ERROR_GET_METRICS', '%s')
57-
E('ERROR_GET_METRIC_BY_ID', '%s')
58-
E('ERROR_GET_REPORT', '%s')
59-
E('ERROR_GET_SEGMENTS', '%s')
60-
E('ERROR_VALIDATE_SEGMENT', '%s')
61-
E('ERROR_GET_USERS', '%s')
62-
E('ERROR_GET_CURRENT_USER', '%s')
63-
E('ERROR_GET_USAGE_LOGS', '%s')
48+
E('ERROR_GET_CALCULATED_METRICS', '%o')
49+
E('ERROR_GET_CALCULATED_METRIC_BY_ID', '%o')
50+
E('ERROR_GET_COLLECTIONS', '%o')
51+
E('ERROR_GET_COLLECTION_BY_ID', '%o')
52+
E('ERROR_GET_DATE_RANGES', '%o')
53+
E('ERROR_GET_DATE_RANGE_BY_ID', '%o')
54+
E('ERROR_GET_DIMENSIONS', '%o')
55+
E('ERROR_GET_DIMENSION_BY_ID', '%o')
56+
E('ERROR_GET_METRICS', '%o')
57+
E('ERROR_GET_METRIC_BY_ID', '%o')
58+
E('ERROR_GET_REPORT', '%o')
59+
E('ERROR_GET_SEGMENTS', '%o')
60+
E('ERROR_VALIDATE_SEGMENT', '%o')
61+
E('ERROR_GET_USERS', '%o')
62+
E('ERROR_GET_CURRENT_USER', '%o')
63+
E('ERROR_GET_USAGE_LOGS', '%o')

test/index.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ test('test getCollectionById', async () => {
119119
res = await checkErrorResponse(api, url, method, new errorSDK.codes.ERROR_GET_COLLECTION_BY_ID(), [123])
120120
mockResponseWithMethod(url, method, mock.errors.Internal_Server_Error.err)
121121
res = await checkErrorResponse(api, url, method, new errorSDK.codes.ERROR_GET_COLLECTION_BY_ID(), [123])
122+
mockResponseWithMethod(url, method, mock.errors.Resource_Not_Found.err)
123+
res = await checkErrorResponse(api, url, method, new errorSDK.codes.ERROR_GET_COLLECTION_BY_ID(), [123])
122124
})
123125

124126
test('test getDateRanges', async () => {
@@ -155,6 +157,8 @@ test('test getDateRangeById', async () => {
155157
// check error responses
156158
mockResponseWithMethod(url, method, mock.errors.Internal_Server_Error.err)
157159
res = await checkErrorResponse(api, url, method, new errorSDK.codes.ERROR_GET_DATE_RANGE_BY_ID(), [123])
160+
mockResponseWithMethod(url, method, mock.errors.Resource_Not_Found.err)
161+
res = await checkErrorResponse(api, url, method, new errorSDK.codes.ERROR_GET_DATE_RANGE_BY_ID(), [123])
158162
})
159163

160164
test('test getDimensions', async () => {
@@ -239,6 +243,8 @@ test('test getMetricById', async () => {
239243
// check error responses
240244
mockResponseWithMethod(url, method, mock.errors.Unauthorized_Request.err)
241245
res = await checkErrorResponse(api, url, method, new errorSDK.codes.ERROR_GET_METRIC_BY_ID(), [111, 123])
246+
mockResponseWithMethod(url, method, mock.errors.Resource_Not_Found.err)
247+
res = await checkErrorResponse(api, url, method, new errorSDK.codes.ERROR_GET_METRIC_BY_ID(), [111, 123])
242248
})
243249

244250
test('test getReport', async () => {
@@ -255,6 +261,8 @@ test('test getReport', async () => {
255261
// check error responses
256262
mockResponseWithMethod(url, method, mock.errors.Bad_Request.err)
257263
res = await checkErrorResponse(api, url, method, new errorSDK.codes.ERROR_GET_REPORT(), [{}])
264+
mockResponseWithMethod(url, method, mock.errors.Invalid_Parameter.err)
265+
res = await checkErrorResponse(api, url, method, new errorSDK.codes.ERROR_GET_REPORT(), [{}])
258266
})
259267

260268
test('test getSegments', async () => {
@@ -397,6 +405,7 @@ function checkErrorResponse (fn, url, method, error, args = []) {
397405
.catch(e => {
398406
expect(e.name).toEqual(error.name)
399407
expect(e.code).toEqual(error.code)
408+
expect(e.message).not.toContain('response: [Object]')
400409
resolve()
401410
})
402411
})

test/mock.js

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,76 @@ OF ANY KIND, either express or implied. See the License for the specific languag
99
governing permissions and limitations under the License.
1010
*/
1111

12+
class InvalidParametersError extends Error {
13+
constructor () {
14+
super('Bad Request')
15+
this.status = 400
16+
this.statusCode = 400
17+
this.response = {
18+
status: 400,
19+
statusCode: 400,
20+
response: {
21+
ok: false,
22+
url: 'https://analytics.adobe.io/api/test',
23+
status: 400,
24+
statusText: 'Bad Request',
25+
headers: {},
26+
text: '{"errorCode":"invalid_parameters","errorDescription":"The passed parameter is not valid","errorId":"e9610fe6-e700-4644-835a-33ed517572f2"}',
27+
data: '{"errorCode":"invalid_parameters","errorDescription":"The passed parameter is not valid","errorId":"e9610fe6-e700-4644-835a-33ed517572f2"}',
28+
body: {
29+
errorCode: 'invalid_parameters',
30+
errorDescription: 'The passed parameter is not valid',
31+
errorId: 'e9610fe6-e700-4644-835a-33ed517572f2'
32+
},
33+
obj: {
34+
errorCode: 'invalid_parameters',
35+
errorDescription: 'The passed parameter is not valid',
36+
errorId: 'e9610fe6-e700-4644-835a-33ed517572f2'
37+
}
38+
}
39+
}
40+
}
41+
}
42+
43+
class ResourceNotFoundError extends Error {
44+
constructor () {
45+
super('Not Found')
46+
this.status = 404
47+
this.statusCode = 404
48+
this.response = {
49+
ok: false,
50+
url: 'https://analytics.adobe.io/api/test',
51+
status: 404,
52+
statusText: 'Not Found',
53+
headers: {},
54+
text: '{"errorCode":"resource_not_found","errorDescription":"The asked resource was not found","errorId":"fb62df94-01a2-446b-b69d-024efe1afade"}',
55+
data: '{"errorCode":"resource_not_found","errorDescription":"The asked resource was not found","errorId":"fb62df94-01a2-446b-b69d-024efe1afade"}',
56+
body: {
57+
errorCode: 'resource_not_found',
58+
errorDescription: 'The asked resource was not found',
59+
errorId: 'fb62df94-01a2-446b-b69d-024efe1afade'
60+
},
61+
obj: {
62+
errorCode: 'resource_not_found',
63+
errorDescription: 'The asked resource was not found',
64+
errorId: 'fb62df94-01a2-446b-b69d-024efe1afade'
65+
}
66+
}
67+
}
68+
}
69+
1270
const BadRequest = {
1371
err: {
1472
throws: new Error('Bad Request')
1573
},
1674
message: 'Bad Request'
1775

1876
}
77+
const InvalidParameter = {
78+
err: {
79+
throws: new InvalidParametersError()
80+
}
81+
}
1982
const UnauthorizedRequest = {
2083
err: {
2184
throws: new Error('Unauthorized')
@@ -34,6 +97,11 @@ const NotFound = {
3497
},
3598
message: 'Not Found'
3699
}
100+
const ResourceNotFound = {
101+
err: {
102+
throws: new ResourceNotFoundError()
103+
}
104+
}
37105
const InternalServerError = {
38106
err: {
39107
throws: new Error('Internal Server Error')
@@ -552,6 +620,8 @@ module.exports = {
552620
Unauthorized_Request: UnauthorizedRequest,
553621
Forbidden_Request: ForbiddenRequest,
554622
Not_Found: NotFound,
555-
Internal_Server_Error: InternalServerError
623+
Internal_Server_Error: InternalServerError,
624+
Invalid_Parameter: InvalidParameter,
625+
Resource_Not_Found: ResourceNotFound
556626
}
557627
}

0 commit comments

Comments
 (0)