-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into port-manager
- Loading branch information
Showing
22 changed files
with
1,316 additions
and
90 deletions.
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
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(); | ||
expect(() => throwErrorWithMessage('', {}, error)).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('throwTypeErrorWithMessage', () => { | ||
it('throws type error with message', () => { | ||
const error = newError(); | ||
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(); | ||
expect(() => throwError(error)).toThrow(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.