-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tests for apiErrors and standardErrors #48
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
import { | ||
isApiStatusCodeError, | ||
isMissingScopeError, | ||
isGatingError, | ||
isApiUploadValidationError, | ||
isSpecifiedHubSpotAuthError, | ||
throwStatusCodeError, | ||
throwApiStatusCodeError, | ||
throwApiError, | ||
throwApiUploadError, | ||
} from '../apiErrors'; | ||
import { BaseError, GenericError, StatusCodeError } from '../../types/Error'; | ||
|
||
export const newError = (overrides = {}): BaseError => { | ||
return { | ||
name: 'Error', | ||
message: 'An error ocurred', | ||
statusCode: 200, | ||
errors: [], | ||
...overrides, | ||
}; | ||
}; | ||
|
||
export const newStatutsCodeError = (overrides = {}): GenericError => { | ||
return { | ||
...newError(), | ||
name: 'StatusCodeError', | ||
response: { | ||
request: { | ||
href: 'http://example.com/', | ||
method: 'GET', | ||
}, | ||
body: {}, | ||
headers: {}, | ||
statusCode: 200, | ||
}, | ||
...overrides, | ||
}; | ||
}; | ||
|
||
describe('apiErrors', () => { | ||
describe('isApiStatusCodeError', () => { | ||
it('returns true for api status code errors', () => { | ||
const error1 = newError({ statusCode: 100 }); | ||
const error2 = newError({ statusCode: 599 }); | ||
const error3 = newStatutsCodeError({ statusCode: 99 }); | ||
expect(isApiStatusCodeError(error1)).toBe(true); | ||
expect(isApiStatusCodeError(error2)).toBe(true); | ||
expect(isApiStatusCodeError(error3)).toBe(true); | ||
}); | ||
|
||
it('returns false for non api status code errors', () => { | ||
const error1 = newError({ statusCode: 99 }); | ||
const error2 = newError({ statusCode: 600 }); | ||
expect(isApiStatusCodeError(error1)).toBe(false); | ||
expect(isApiStatusCodeError(error2)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('isMissingScopeError', () => { | ||
it('returns true for missing scope errors', () => { | ||
const error1 = newStatutsCodeError({ | ||
statusCode: 403, | ||
error: { category: 'MISSING_SCOPES' }, | ||
}); | ||
expect(isMissingScopeError(error1)).toBe(true); | ||
}); | ||
|
||
it('returns false for non missing scope errors', () => { | ||
const error1 = newStatutsCodeError({ | ||
statusCode: 400, | ||
error: { category: 'MISSING_SCOPES' }, | ||
}); | ||
const error2 = newStatutsCodeError({ name: 'NonStatusCodeError' }); | ||
expect(isMissingScopeError(error1)).toBe(false); | ||
expect(isMissingScopeError(error2)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('isGatingError', () => { | ||
it('returns true for gating errors', () => { | ||
const error1 = newStatutsCodeError({ | ||
statusCode: 403, | ||
error: { category: 'GATED' }, | ||
}); | ||
expect(isGatingError(error1)).toBe(true); | ||
}); | ||
|
||
it('returns false for non gating errors', () => { | ||
const error1 = newStatutsCodeError({ | ||
statusCode: 400, | ||
error: { category: 'GATED' }, | ||
}); | ||
const error2 = newStatutsCodeError({ name: 'NonStatusCodeError' }); | ||
expect(isGatingError(error1)).toBe(false); | ||
expect(isGatingError(error2)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('isApiUploadValidationError', () => { | ||
it('returns true for api upload validation errors', () => { | ||
const error1 = newStatutsCodeError({ | ||
statusCode: 400, | ||
response: { body: { message: 'upload validation error' } }, | ||
}); | ||
const error2 = newStatutsCodeError({ | ||
statusCode: 400, | ||
response: { body: { errors: [] } }, | ||
}); | ||
expect(isApiUploadValidationError(error1)).toBe(true); | ||
expect(isApiUploadValidationError(error2)).toBe(true); | ||
}); | ||
|
||
it('returns false for non api upload validation errors', () => { | ||
const error1 = newStatutsCodeError({ | ||
statusCode: 400, | ||
response: { body: null }, | ||
}); | ||
const error2 = newStatutsCodeError({ name: 'NonStatusCodeError' }); | ||
expect(isApiUploadValidationError(error1)).toBe(false); | ||
expect(isApiUploadValidationError(error2)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('isSpecifiedHubSpotAuthError', () => { | ||
it('returns true for matching HubSpot auth errors', () => { | ||
const error1 = newError({ name: 'HubSpotAuthError', statusCode: 123 }); | ||
expect(isSpecifiedHubSpotAuthError(error1, { statusCode: 123 })).toBe( | ||
true | ||
); | ||
}); | ||
|
||
it('returns false for non matching HubSpot auth errors', () => { | ||
const error1 = newError({ name: 'HubSpotAuthError', statusCode: 123 }); | ||
expect(isSpecifiedHubSpotAuthError(error1, { statusCode: 124 })).toBe( | ||
false | ||
); | ||
}); | ||
}); | ||
|
||
describe('throwStatusCodeError', () => { | ||
it('throws status code error', () => { | ||
const error = newStatutsCodeError() as StatusCodeError; | ||
expect(() => throwStatusCodeError(error)).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('throwApiStatusCodeError', () => { | ||
it('throws api status code error', () => { | ||
const error = newStatutsCodeError() as StatusCodeError; | ||
expect(() => throwApiStatusCodeError(error)).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('throwApiError', () => { | ||
it('throws api error', () => { | ||
const error = newStatutsCodeError() as StatusCodeError; | ||
expect(() => throwApiError(error)).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('throwApiUploadError', () => { | ||
it('throws api upload error', () => { | ||
const error = newStatutsCodeError() as StatusCodeError; | ||
expect(() => throwApiUploadError(error)).toThrow(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
isSystemError, | ||
isFatalError, | ||
throwErrorWithMessage, | ||
throwTypeErrorWithMessage, | ||
throwAuthErrorWithMessage, | ||
throwError, | ||
} from '../standardErrors'; | ||
import { BaseError, StatusCodeError } from '../../types/Error'; | ||
import { HubSpotAuthError } from '../../models/HubSpotAuthError'; | ||
|
||
export const newError = (overrides = {}): BaseError => { | ||
return { | ||
name: 'Error', | ||
message: 'An error ocurred', | ||
errno: 1, | ||
code: 'error_code', | ||
syscall: 'error_syscall', | ||
errors: [], | ||
...overrides, | ||
}; | ||
}; | ||
|
||
describe('standardErrors', () => { | ||
describe('isSystemError', () => { | ||
it('returns true for system errors', () => { | ||
const error = newError(); | ||
expect(isSystemError(error)).toBe(true); | ||
}); | ||
|
||
it('returns false for non system errors', () => { | ||
const error1 = newError({ errno: null }); | ||
const error2 = newError({ code: null }); | ||
const error3 = newError({ syscall: null }); | ||
expect(isSystemError(error1)).toBe(false); | ||
expect(isSystemError(error2)).toBe(false); | ||
expect(isSystemError(error3)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('isFatalError', () => { | ||
it('returns true for fatal errors', () => { | ||
const cause = newError() as StatusCodeError; | ||
const error = new HubSpotAuthError('A fatal auth error', { cause }); | ||
expect(isFatalError(error)).toBe(true); | ||
}); | ||
|
||
it('returns false for non fatal errors', () => { | ||
const error = newError(); | ||
expect(isFatalError(error)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('throwErrorWithMessage', () => { | ||
it('throws error with message', () => { | ||
const error = newError() as BaseError; | ||
expect(() => throwErrorWithMessage('', {}, error)).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('throwTypeErrorWithMessage', () => { | ||
it('throws type error with message', () => { | ||
const error = newError() as BaseError; | ||
expect(() => throwTypeErrorWithMessage('', {}, error)).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('throwAuthErrorWithMessage', () => { | ||
it('throws auth error with message', () => { | ||
const error = newError() as StatusCodeError; | ||
expect(() => throwAuthErrorWithMessage('', {}, error)).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('throwError', () => { | ||
it('throws error', () => { | ||
const error = newError() as BaseError; | ||
expect(() => throwError(error)).toThrow(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,60 @@ | ||
import { StatusCodeError, StatusCodeErrorContext } from '../types/Error'; | ||
import { | ||
GenericError, | ||
StatusCodeError, | ||
StatusCodeErrorContext, | ||
} from '../types/Error'; | ||
import { HTTP_METHOD_VERBS, HTTP_METHOD_PREPOSITIONS } from '../constants/api'; | ||
import { i18n } from '../utils/lang'; | ||
import { throwError } from './standardErrors'; | ||
import { HubSpotAuthError } from './HubSpotAuthError'; | ||
import { HubSpotAuthError } from '../models/HubSpotAuthError'; | ||
|
||
function isApiStatusCodeError(err: StatusCodeError) { | ||
export function isApiStatusCodeError(err: GenericError): boolean { | ||
return ( | ||
err.name === 'StatusCodeError' || | ||
(err.statusCode && err.statusCode >= 100 && err.statusCode < 600) | ||
(!!err.statusCode && err.statusCode >= 100 && err.statusCode < 600) | ||
); | ||
} | ||
|
||
export function isMissingScopeError(err: StatusCodeError): boolean { | ||
return Boolean( | ||
err.name === 'StatusCodeError' && | ||
err.statusCode === 403 && | ||
err.error && | ||
err.error.category === 'MISSING_SCOPES' | ||
export function isMissingScopeError(err: GenericError): boolean { | ||
return ( | ||
isApiStatusCodeError(err) && | ||
err.statusCode === 403 && | ||
!!err.error && | ||
err.error.category === 'MISSING_SCOPES' | ||
); | ||
} | ||
|
||
export function isGatingError(err: StatusCodeError): boolean { | ||
return Boolean( | ||
err.name === 'StatusCodeError' && | ||
err.statusCode === 403 && | ||
err.error && | ||
err.error.category === 'GATED' | ||
export function isGatingError(err: GenericError): boolean { | ||
return ( | ||
isApiStatusCodeError(err) && | ||
err.statusCode === 403 && | ||
!!err.error && | ||
err.error.category === 'GATED' | ||
); | ||
} | ||
|
||
function isApiUploadValidationError(err: StatusCodeError): boolean { | ||
return Boolean( | ||
export function isApiUploadValidationError(err: GenericError): boolean { | ||
return ( | ||
isApiStatusCodeError(err) && | ||
err.statusCode === 400 && | ||
err.response && | ||
err.response.body && | ||
(err.response.body.message || err.response.body.errors) | ||
!!err.response && | ||
!!err.response.body && | ||
!!(err.response.body.message || !!err.response.body.errors) | ||
); | ||
} | ||
|
||
export function isSpecifiedHubSpotAuthError( | ||
err: HubSpotAuthError, | ||
err: GenericError, | ||
{ statusCode, category, subCategory }: Partial<HubSpotAuthError> | ||
): boolean { | ||
const statusCodeErr = !statusCode || err.statusCode === statusCode; | ||
const categoryErr = !category || err.category === category; | ||
const subCategoryErr = !subCategory || err.subCategory === subCategory; | ||
return ( | ||
return Boolean( | ||
err.name === 'HubSpotAuthError' && | ||
statusCodeErr && | ||
categoryErr && | ||
subCategoryErr | ||
statusCodeErr && | ||
categoryErr && | ||
subCategoryErr | ||
); | ||
} | ||
|
||
|
@@ -89,6 +94,9 @@ function logValidationErrors(error: StatusCodeError) { | |
} | ||
} | ||
|
||
/** | ||
* @throws | ||
*/ | ||
export function throwStatusCodeError( | ||
error: StatusCodeError, | ||
context: StatusCodeErrorContext = {} | ||
|
@@ -106,11 +114,14 @@ export function throwStatusCodeError( | |
throw new Error(errorData, { cause: error }); | ||
} | ||
|
||
/** | ||
* @throws | ||
*/ | ||
export function throwApiStatusCodeError( | ||
error: StatusCodeError, | ||
context: StatusCodeErrorContext | ||
context: StatusCodeErrorContext = {} | ||
): never { | ||
const i18nKey = 'errors.api'; | ||
const i18nKey = 'errors.errorTypes.api'; | ||
const { statusCode } = error; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed we were getting missing translation errors here |
||
const { method } = error.options || {}; | ||
const { projectName } = context; | ||
|
@@ -213,19 +224,25 @@ export function throwApiStatusCodeError( | |
throwError(new Error(errorMessage.join(' '), { cause: error })); | ||
} | ||
|
||
/** | ||
* @throws | ||
*/ | ||
export function throwApiError( | ||
error: StatusCodeError, | ||
context: StatusCodeErrorContext | ||
context: StatusCodeErrorContext = {} | ||
): never { | ||
if (isApiStatusCodeError(error)) { | ||
throwApiStatusCodeError(error, context); | ||
} | ||
throwError(error); | ||
} | ||
|
||
/** | ||
* @throws | ||
*/ | ||
export function throwApiUploadError( | ||
error: StatusCodeError, | ||
context: StatusCodeErrorContext | ||
context: StatusCodeErrorContext = {} | ||
): never { | ||
if (isApiUploadValidationError(error)) { | ||
logValidationErrors(error); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thinking here is that we aren't sure which type of error we're going to get as an arg. The calling system may be passing something in that's more generic and doesn't necessarily have the type of
StandardCodeError
.