Skip to content

Commit

Permalink
Merge pull request #15 from snyk-tech-services/fix/Expose-response-da…
Browse files Browse the repository at this point in the history
…ta-if-errors

fix: Return data from api along with error
  • Loading branch information
aarlaud authored Jun 15, 2020
2 parents 87b4ebb + 2ba2611 commit 6308ab2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/lib/customErrors/apiError.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@


class ApiError extends Error {
data: {}
constructor(message: any){
super(message)
this.name = "ApiError"
this.message = (message || "")
this.data = (message.response?.data || "")
}
}

class ApiAuthenticationError extends Error {
data: {}
constructor(message: any){
super(message)
this.name = "ApiAuthenticationError"
this.message = (message || "")
this.data = (message.response?.data || "")
}
}

class NotFoundError extends Error {
data: {}
constructor(message: any){
super(message)
this.name = "NotFoundError"
this.message = (message || "")
this.data = (message.response?.data || "")
}
}

class GenericError extends Error {
data: {}
constructor(message: any){
super(message)
this.name = "Unknown"
this.message = (message || "")
this.data = (message.response?.data || "")
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const makeSnykRequest = async (request: snykRequest, snykToken: string = '') =>
case 401:
throw new Error.ApiAuthenticationError(err)
case 404:
throw new Error.NotFoundError("Snyk API - Could not find this resource")
throw new Error.NotFoundError(err)
case 500:
throw new Error.ApiError(err)
default:
Expand Down
8 changes: 8 additions & 0 deletions test/lib/request/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe('Test Snyk Utils error handling/classification', () => {
try {
await makeSnykRequest({ verb: 'GET', url: '/xyz', body: '' });
} catch (err) {
expect(err.data).toEqual(404);
expect(err).toBeInstanceOf(NotFoundError);
}
});
Expand All @@ -111,6 +112,7 @@ describe('Test Snyk Utils error handling/classification', () => {
body: JSON.stringify(bodyToSend),
});
} catch (err) {
expect(err.data).toEqual(404);
expect(err).toBeInstanceOf(NotFoundError);
}
});
Expand All @@ -119,6 +121,7 @@ describe('Test Snyk Utils error handling/classification', () => {
try {
await makeSnykRequest({ verb: 'GET', url: '/apierror' });
} catch (err) {
expect(err.data).toEqual(500);
expect(err).toBeInstanceOf(ApiError);
}
});
Expand All @@ -133,6 +136,7 @@ describe('Test Snyk Utils error handling/classification', () => {
body: JSON.stringify(bodyToSend),
});
} catch (err) {
expect(err.data).toEqual(500);
expect(err).toBeInstanceOf(ApiError);
}
});
Expand All @@ -141,6 +145,7 @@ describe('Test Snyk Utils error handling/classification', () => {
try {
await makeSnykRequest({ verb: 'GET', url: '/apiautherror' });
} catch (err) {
expect(err.data).toEqual(401);
expect(err).toBeInstanceOf(ApiAuthenticationError);
}
});
Expand All @@ -155,6 +160,7 @@ describe('Test Snyk Utils error handling/classification', () => {
body: JSON.stringify(bodyToSend),
});
} catch (err) {
expect(err.data).toEqual(401);
expect(err).toBeInstanceOf(ApiAuthenticationError);
}
});
Expand All @@ -163,6 +169,7 @@ describe('Test Snyk Utils error handling/classification', () => {
try {
await makeSnykRequest({ verb: 'GET', url: '/genericerror' });
} catch (err) {
expect(err.data).toEqual(512);
expect(err).toBeInstanceOf(GenericError);
}
});
Expand All @@ -177,6 +184,7 @@ describe('Test Snyk Utils error handling/classification', () => {
body: JSON.stringify(bodyToSend),
});
} catch (err) {
expect(err.data).toEqual(512);
expect(err).toBeInstanceOf(GenericError);
}
});
Expand Down

0 comments on commit 6308ab2

Please sign in to comment.