-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Make UnionCredentialsExtractor tolerate failures.
Fixes #1031
- Loading branch information
1 parent
a1579f6
commit c13456c
Showing
16 changed files
with
179 additions
and
151 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
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
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
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,38 @@ | ||
import { BadRequestHttpError } from './BadRequestHttpError'; | ||
import { createErrorMessage } from './ErrorUtil'; | ||
import { HttpError } from './HttpError'; | ||
import { InternalServerError } from './InternalServerError'; | ||
|
||
/** | ||
* Returns the HTTP status code corresponding to the error. | ||
*/ | ||
export function getStatusCode(error: Error): number { | ||
return HttpError.isInstance(error) ? error.statusCode : 500; | ||
} | ||
|
||
/** | ||
* Combines a list of errors into a single HttpErrors. | ||
* Status code depends on the input errors. If they all share the same status code that code will be re-used. | ||
* If they are all within the 4xx range, 400 will be used, otherwise 500. | ||
* | ||
* @param errors - Errors to combine. | ||
* @param messagePrefix - Prefix for the aggregate error message. Will be followed with an array of all the messages. | ||
*/ | ||
export function createAggregateError(errors: Error[], messagePrefix = 'No handler supports the given input:'): | ||
HttpError { | ||
const httpErrors = errors.map((error): HttpError => | ||
HttpError.isInstance(error) ? error : new InternalServerError(createErrorMessage(error))); | ||
const joined = httpErrors.map((error: Error): string => error.message).join(', '); | ||
const message = `${messagePrefix} [${joined}]`; | ||
|
||
// Check if all errors have the same status code | ||
if (httpErrors.length > 0 && httpErrors.every((error): boolean => error.statusCode === httpErrors[0].statusCode)) { | ||
return new HttpError(httpErrors[0].statusCode, httpErrors[0].name, message); | ||
} | ||
|
||
// Find the error range (4xx or 5xx) | ||
if (httpErrors.some((error): boolean => error.statusCode >= 500)) { | ||
return new InternalServerError(message); | ||
} | ||
return new BadRequestHttpError(message); | ||
} |
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
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
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
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,50 @@ | ||
import { HttpError } from '../../../../src/util/errors/HttpError'; | ||
import { createAggregateError, getStatusCode } from '../../../../src/util/errors/HttpErrorUtil'; | ||
import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError'; | ||
|
||
describe('ErrorUtil', (): void => { | ||
describe('createAggregateError', (): void => { | ||
const error401 = new HttpError(401, 'UnauthorizedHttpError'); | ||
const error415 = new HttpError(415, 'UnsupportedMediaTypeHttpError'); | ||
const error501 = new HttpError(501, 'NotImplementedHttpError'); | ||
const error = new Error('noStatusCode'); | ||
|
||
it('throws an error with matching status code if all errors have the same.', async(): Promise<void> => { | ||
expect(createAggregateError([ error401, error401 ])).toMatchObject({ | ||
statusCode: 401, | ||
name: 'UnauthorizedHttpError', | ||
}); | ||
}); | ||
|
||
it('throws an InternalServerError if one of the errors has status code 5xx.', async(): Promise<void> => { | ||
expect(createAggregateError([ error401, error501 ])).toMatchObject({ | ||
statusCode: 500, | ||
name: 'InternalServerError', | ||
}); | ||
}); | ||
|
||
it('throws an BadRequestHttpError if all handlers have 4xx status codes.', async(): Promise<void> => { | ||
expect(createAggregateError([ error401, error415 ])).toMatchObject({ | ||
statusCode: 400, | ||
name: 'BadRequestHttpError', | ||
}); | ||
}); | ||
|
||
it('interprets non-HTTP errors as internal errors.', async(): Promise<void> => { | ||
expect(createAggregateError([ error ])).toMatchObject({ | ||
statusCode: 500, | ||
name: 'InternalServerError', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('#getStatusCode', (): void => { | ||
it('returns the corresponding status code for HttpErrors.', async(): Promise<void> => { | ||
expect(getStatusCode(new NotFoundHttpError())).toBe(404); | ||
}); | ||
|
||
it('returns 500 for other errors.', async(): Promise<void> => { | ||
expect(getStatusCode(new Error('404'))).toBe(500); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.