enhancement(auth): better error message handling

Signed-off-by: Erik Michelson <github@erik.michelson.eu>
This commit is contained in:
Erik Michelson 2023-03-25 18:58:48 +01:00
parent 8e57188ab5
commit ca9836d691
37 changed files with 199 additions and 207 deletions

View file

@ -25,7 +25,7 @@ export abstract class ApiRequestBuilder<ResponseType> {
*
* @param endpoint The target endpoint without a leading slash.
*/
constructor(endpoint: string, private apiI18nKey: string) {
constructor(endpoint: string) {
this.targetUrl = `api/private/${endpoint}`
}
@ -38,9 +38,8 @@ export abstract class ApiRequestBuilder<ResponseType> {
})
if (response.status >= 400) {
const apiErrorResponse = await this.readApiErrorResponseFromBody(response)
const statusText = response.status === 400 ? apiErrorResponse?.error ?? 'unknown' : response.statusText
throw new ApiError(response.status, statusText, this.apiI18nKey, apiErrorResponse?.error)
const backendError = await this.readApiErrorResponseFromBody(response)
throw new ApiError(response.status, backendError?.name, backendError?.message)
}
return new ApiResponse(response)

View file

@ -21,7 +21,7 @@ describe('DeleteApiRequestBuilder', () => {
describe('sendRequest without body', () => {
it('without headers', async () => {
expectFetch('api/private/test', 204, { method: 'DELETE' })
await new DeleteApiRequestBuilder<string, undefined>('test', 'test').sendRequest()
await new DeleteApiRequestBuilder<string, undefined>('test').sendRequest()
})
it('with single header', async () => {
@ -31,7 +31,7 @@ describe('DeleteApiRequestBuilder', () => {
method: 'DELETE',
headers: expectedHeaders
})
await new DeleteApiRequestBuilder<string, undefined>('test', 'test').withHeader('test', 'true').sendRequest()
await new DeleteApiRequestBuilder<string, undefined>('test').withHeader('test', 'true').sendRequest()
})
it('with overriding single header', async () => {
@ -41,7 +41,7 @@ describe('DeleteApiRequestBuilder', () => {
method: 'DELETE',
headers: expectedHeaders
})
await new DeleteApiRequestBuilder<string, undefined>('test', 'test')
await new DeleteApiRequestBuilder<string, undefined>('test')
.withHeader('test', 'true')
.withHeader('test', 'false')
.sendRequest()
@ -55,7 +55,7 @@ describe('DeleteApiRequestBuilder', () => {
method: 'DELETE',
headers: expectedHeaders
})
await new DeleteApiRequestBuilder<string, undefined>('test', 'test')
await new DeleteApiRequestBuilder<string, undefined>('test')
.withHeader('test', 'true')
.withHeader('test2', 'false')
.sendRequest()
@ -71,7 +71,7 @@ describe('DeleteApiRequestBuilder', () => {
headers: expectedHeaders,
body: '{"test":true,"foo":"bar"}'
})
await new DeleteApiRequestBuilder('test', 'test')
await new DeleteApiRequestBuilder('test')
.withJsonBody({
test: true,
foo: 'bar'
@ -84,7 +84,7 @@ describe('DeleteApiRequestBuilder', () => {
method: 'DELETE',
body: 'HedgeDoc'
})
await new DeleteApiRequestBuilder('test', 'test').withBody('HedgeDoc').sendRequest()
await new DeleteApiRequestBuilder('test').withBody('HedgeDoc').sendRequest()
})
describe('sendRequest with custom options', () => {
@ -93,7 +93,7 @@ describe('DeleteApiRequestBuilder', () => {
method: 'DELETE',
cache: 'force-cache'
})
await new DeleteApiRequestBuilder<string, undefined>('test', 'test')
await new DeleteApiRequestBuilder<string, undefined>('test')
.withCustomOptions({
cache: 'force-cache'
})
@ -105,7 +105,7 @@ describe('DeleteApiRequestBuilder', () => {
method: 'DELETE',
cache: 'no-store'
})
await new DeleteApiRequestBuilder<string, undefined>('test', 'test')
await new DeleteApiRequestBuilder<string, undefined>('test')
.withCustomOptions({
cache: 'force-cache'
})
@ -121,7 +121,7 @@ describe('DeleteApiRequestBuilder', () => {
cache: 'force-cache',
integrity: 'test'
})
await new DeleteApiRequestBuilder<string, undefined>('test', 'test')
await new DeleteApiRequestBuilder<string, undefined>('test')
.withCustomOptions({
cache: 'force-cache',
integrity: 'test'
@ -131,28 +131,28 @@ describe('DeleteApiRequestBuilder', () => {
})
describe('failing sendRequest', () => {
it('with bad request without api error name', async () => {
it('without backend provided error name or error message', async () => {
expectFetch('api/private/test', 400, { method: 'DELETE' })
const request = new DeleteApiRequestBuilder<string>('test', 'test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(400, 'unknown', 'test', 'testExplosion'))
const request = new DeleteApiRequestBuilder<string>('test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(400, undefined, undefined))
})
it('with bad request with api error name', async () => {
it('with backend error name and error message', async () => {
expectFetch('api/private/test', 400, { method: 'DELETE' }, {
message: 'The API has exploded!',
error: 'testExplosion'
name: 'testExplosion'
} as ApiErrorResponse)
const request = new DeleteApiRequestBuilder<string>('test', 'test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(400, 'testExplosion', 'test', 'testExplosion'))
const request = new DeleteApiRequestBuilder<string>('test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(400, 'testExplosion', 'The API has exploded!'))
})
it('with non bad request error', async () => {
it('with another status code than 400', async () => {
expectFetch('api/private/test', 401, { method: 'DELETE' }, {
message: 'The API has exploded!',
error: 'testExplosion'
name: 'testExplosion'
} as ApiErrorResponse)
const request = new DeleteApiRequestBuilder<string>('test', 'test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(401, 'forbidden', 'test', 'testExplosion'))
const request = new DeleteApiRequestBuilder<string>('test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(401, 'testExplosion', 'The API has exploded!'))
})
})
})

View file

@ -22,7 +22,7 @@ describe('GetApiRequestBuilder', () => {
describe('sendRequest', () => {
it('without headers', async () => {
expectFetch('api/private/test', 200, { method: 'GET' })
await new GetApiRequestBuilder<string>('test', 'test').sendRequest()
await new GetApiRequestBuilder<string>('test').sendRequest()
})
it('with single header', async () => {
@ -32,7 +32,7 @@ describe('GetApiRequestBuilder', () => {
method: 'GET',
headers: expectedHeaders
})
await new GetApiRequestBuilder<string>('test', 'test').withHeader('test', 'true').sendRequest()
await new GetApiRequestBuilder<string>('test').withHeader('test', 'true').sendRequest()
})
it('with overriding single header', async () => {
@ -42,7 +42,7 @@ describe('GetApiRequestBuilder', () => {
method: 'GET',
headers: expectedHeaders
})
await new GetApiRequestBuilder<string>('test', 'test')
await new GetApiRequestBuilder<string>('test')
.withHeader('test', 'true')
.withHeader('test', 'false')
.sendRequest()
@ -56,7 +56,7 @@ describe('GetApiRequestBuilder', () => {
method: 'GET',
headers: expectedHeaders
})
await new GetApiRequestBuilder<string>('test', 'test')
await new GetApiRequestBuilder<string>('test')
.withHeader('test', 'true')
.withHeader('test2', 'false')
.sendRequest()
@ -69,7 +69,7 @@ describe('GetApiRequestBuilder', () => {
method: 'GET',
cache: 'force-cache'
})
await new GetApiRequestBuilder<string>('test', 'test')
await new GetApiRequestBuilder<string>('test')
.withCustomOptions({
cache: 'force-cache'
})
@ -81,7 +81,7 @@ describe('GetApiRequestBuilder', () => {
method: 'GET',
cache: 'no-store'
})
await new GetApiRequestBuilder<string>('test', 'test')
await new GetApiRequestBuilder<string>('test')
.withCustomOptions({
cache: 'force-cache'
})
@ -97,7 +97,7 @@ describe('GetApiRequestBuilder', () => {
cache: 'force-cache',
integrity: 'test'
})
await new GetApiRequestBuilder<string>('test', 'test')
await new GetApiRequestBuilder<string>('test')
.withCustomOptions({
cache: 'force-cache',
integrity: 'test'
@ -107,28 +107,28 @@ describe('GetApiRequestBuilder', () => {
})
describe('failing sendRequest', () => {
it('with bad request without api error name', async () => {
it('without backend provided error name or error message', async () => {
expectFetch('api/private/test', 400, { method: 'GET' })
const request = new GetApiRequestBuilder<string>('test', 'test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(400, 'unknown', 'test', 'testExplosion'))
const request = new GetApiRequestBuilder<string>('test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(400, undefined, undefined))
})
it('with bad request with api error name', async () => {
it('with backend error name and error message', async () => {
expectFetch('api/private/test', 400, { method: 'GET' }, {
message: 'The API has exploded!',
error: 'testExplosion'
name: 'testExplosion'
} as ApiErrorResponse)
const request = new GetApiRequestBuilder<string>('test', 'test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(400, 'testExplosion', 'test', 'testExplosion'))
const request = new GetApiRequestBuilder<string>('test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(400, 'testExplosion', 'The API has exploded!'))
})
it('with non bad request error', async () => {
it('with another status code than 400', async () => {
expectFetch('api/private/test', 401, { method: 'GET' }, {
message: 'The API has exploded!',
error: 'testExplosion'
name: 'testExplosion'
} as ApiErrorResponse)
const request = new GetApiRequestBuilder<string>('test', 'test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(401, 'forbidden', 'test', 'testExplosion'))
const request = new GetApiRequestBuilder<string>('test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(401, 'testExplosion', 'The API has exploded!'))
})
})
})

View file

@ -22,7 +22,7 @@ describe('PostApiRequestBuilder', () => {
describe('sendRequest without body', () => {
it('without headers', async () => {
expectFetch('api/private/test', 201, { method: 'POST' })
await new PostApiRequestBuilder<string, undefined>('test', 'test').sendRequest()
await new PostApiRequestBuilder<string, undefined>('test').sendRequest()
})
it('with single header', async () => {
@ -32,7 +32,7 @@ describe('PostApiRequestBuilder', () => {
method: 'POST',
headers: expectedHeaders
})
await new PostApiRequestBuilder<string, undefined>('test', 'test').withHeader('test', 'true').sendRequest()
await new PostApiRequestBuilder<string, undefined>('test').withHeader('test', 'true').sendRequest()
})
it('with overriding single header', async () => {
@ -42,7 +42,7 @@ describe('PostApiRequestBuilder', () => {
method: 'POST',
headers: expectedHeaders
})
await new PostApiRequestBuilder<string, undefined>('test', 'test')
await new PostApiRequestBuilder<string, undefined>('test')
.withHeader('test', 'true')
.withHeader('test', 'false')
.sendRequest()
@ -56,7 +56,7 @@ describe('PostApiRequestBuilder', () => {
method: 'POST',
headers: expectedHeaders
})
await new PostApiRequestBuilder<string, undefined>('test', 'test')
await new PostApiRequestBuilder<string, undefined>('test')
.withHeader('test', 'true')
.withHeader('test2', 'false')
.sendRequest()
@ -72,7 +72,7 @@ describe('PostApiRequestBuilder', () => {
headers: expectedHeaders,
body: '{"test":true,"foo":"bar"}'
})
await new PostApiRequestBuilder('test', 'test')
await new PostApiRequestBuilder('test')
.withJsonBody({
test: true,
foo: 'bar'
@ -85,7 +85,7 @@ describe('PostApiRequestBuilder', () => {
method: 'POST',
body: 'HedgeDoc'
})
await new PostApiRequestBuilder('test', 'test').withBody('HedgeDoc').sendRequest()
await new PostApiRequestBuilder('test').withBody('HedgeDoc').sendRequest()
})
describe('sendRequest with custom options', () => {
@ -94,7 +94,7 @@ describe('PostApiRequestBuilder', () => {
method: 'POST',
cache: 'force-cache'
})
await new PostApiRequestBuilder<string, undefined>('test', 'test')
await new PostApiRequestBuilder<string, undefined>('test')
.withCustomOptions({
cache: 'force-cache'
})
@ -106,7 +106,7 @@ describe('PostApiRequestBuilder', () => {
method: 'POST',
cache: 'no-store'
})
await new PostApiRequestBuilder<string, undefined>('test', 'test')
await new PostApiRequestBuilder<string, undefined>('test')
.withCustomOptions({
cache: 'force-cache'
})
@ -122,7 +122,7 @@ describe('PostApiRequestBuilder', () => {
cache: 'force-cache',
integrity: 'test'
})
await new PostApiRequestBuilder<string, undefined>('test', 'test')
await new PostApiRequestBuilder<string, undefined>('test')
.withCustomOptions({
cache: 'force-cache',
integrity: 'test'
@ -132,28 +132,28 @@ describe('PostApiRequestBuilder', () => {
})
describe('failing sendRequest', () => {
it('with bad request without api error name', async () => {
it('without backend provided error name or error message', async () => {
expectFetch('api/private/test', 400, { method: 'POST' })
const request = new PostApiRequestBuilder<string, string>('test', 'test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(400, 'unknown', 'test', 'testExplosion'))
const request = new PostApiRequestBuilder<string, string>('test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(400, undefined, undefined))
})
it('with bad request with api error name', async () => {
it('with backend error name and error message', async () => {
expectFetch('api/private/test', 400, { method: 'POST' }, {
message: 'The API has exploded!',
error: 'testExplosion'
name: 'testExplosion'
} as ApiErrorResponse)
const request = new PostApiRequestBuilder<string, string>('test', 'test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(400, 'testExplosion', 'test', 'testExplosion'))
const request = new PostApiRequestBuilder<string, string>('test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(400, 'testExplosion', 'The API has exploded!'))
})
it('with non bad request error', async () => {
it('with another status code than 400', async () => {
expectFetch('api/private/test', 401, { method: 'POST' }, {
message: 'The API has exploded!',
error: 'testExplosion'
name: 'testExplosion'
} as ApiErrorResponse)
const request = new PostApiRequestBuilder<string, string>('test', 'test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(401, 'forbidden', 'test', 'testExplosion'))
const request = new PostApiRequestBuilder<string, string>('test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(401, 'testExplosion', 'The API has exploded!'))
})
})
})

View file

@ -22,7 +22,7 @@ describe('PutApiRequestBuilder', () => {
describe('sendRequest without body', () => {
it('without headers', async () => {
expectFetch('api/private/test', 200, { method: 'PUT' })
await new PutApiRequestBuilder<string, undefined>('test', 'test').sendRequest()
await new PutApiRequestBuilder<string, undefined>('test').sendRequest()
})
it('with single header', async () => {
@ -32,7 +32,7 @@ describe('PutApiRequestBuilder', () => {
method: 'PUT',
headers: expectedHeaders
})
await new PutApiRequestBuilder<string, undefined>('test', 'test').withHeader('test', 'true').sendRequest()
await new PutApiRequestBuilder<string, undefined>('test').withHeader('test', 'true').sendRequest()
})
it('with overriding single header', async () => {
@ -42,7 +42,7 @@ describe('PutApiRequestBuilder', () => {
method: 'PUT',
headers: expectedHeaders
})
await new PutApiRequestBuilder<string, undefined>('test', 'test')
await new PutApiRequestBuilder<string, undefined>('test')
.withHeader('test', 'true')
.withHeader('test', 'false')
.sendRequest()
@ -56,7 +56,7 @@ describe('PutApiRequestBuilder', () => {
method: 'PUT',
headers: expectedHeaders
})
await new PutApiRequestBuilder<string, undefined>('test', 'test')
await new PutApiRequestBuilder<string, undefined>('test')
.withHeader('test', 'true')
.withHeader('test2', 'false')
.sendRequest()
@ -72,7 +72,7 @@ describe('PutApiRequestBuilder', () => {
headers: expectedHeaders,
body: '{"test":true,"foo":"bar"}'
})
await new PutApiRequestBuilder('test', 'test')
await new PutApiRequestBuilder('test')
.withJsonBody({
test: true,
foo: 'bar'
@ -85,7 +85,7 @@ describe('PutApiRequestBuilder', () => {
method: 'PUT',
body: 'HedgeDoc'
})
await new PutApiRequestBuilder('test', 'test').withBody('HedgeDoc').sendRequest()
await new PutApiRequestBuilder('test').withBody('HedgeDoc').sendRequest()
})
describe('sendRequest with custom options', () => {
@ -94,7 +94,7 @@ describe('PutApiRequestBuilder', () => {
method: 'PUT',
cache: 'force-cache'
})
await new PutApiRequestBuilder<string, undefined>('test', 'test')
await new PutApiRequestBuilder<string, undefined>('test')
.withCustomOptions({
cache: 'force-cache'
})
@ -106,7 +106,7 @@ describe('PutApiRequestBuilder', () => {
method: 'PUT',
cache: 'no-store'
})
await new PutApiRequestBuilder<string, undefined>('test', 'test')
await new PutApiRequestBuilder<string, undefined>('test')
.withCustomOptions({
cache: 'force-cache'
})
@ -122,7 +122,7 @@ describe('PutApiRequestBuilder', () => {
cache: 'force-cache',
integrity: 'test'
})
await new PutApiRequestBuilder<string, undefined>('test', 'test')
await new PutApiRequestBuilder<string, undefined>('test')
.withCustomOptions({
cache: 'force-cache',
integrity: 'test'
@ -132,28 +132,28 @@ describe('PutApiRequestBuilder', () => {
})
describe('failing sendRequest', () => {
it('with bad request without api error name', async () => {
it('without backend provided error name or error message', async () => {
expectFetch('api/private/test', 400, { method: 'PUT' })
const request = new PutApiRequestBuilder<string, string>('test', 'test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(400, 'unknown', 'test', 'testExplosion'))
const request = new PutApiRequestBuilder<string, undefined>('test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(400, undefined, undefined))
})
it('with bad request with api error name', async () => {
it('with backend error name and error message', async () => {
expectFetch('api/private/test', 400, { method: 'PUT' }, {
message: 'The API has exploded!',
error: 'testExplosion'
name: 'testExplosion'
} as ApiErrorResponse)
const request = new PutApiRequestBuilder<string, string>('test', 'test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(400, 'testExplosion', 'test', 'testExplosion'))
const request = new PutApiRequestBuilder<string, undefined>('test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(400, 'testExplosion', 'The API has exploded!'))
})
it('with non bad request error', async () => {
it('with another status code than 400', async () => {
expectFetch('api/private/test', 401, { method: 'PUT' }, {
message: 'The API has exploded!',
error: 'testExplosion'
name: 'testExplosion'
} as ApiErrorResponse)
const request = new PutApiRequestBuilder<string, string>('test', 'test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(401, 'forbidden', 'test', 'testExplosion'))
const request = new PutApiRequestBuilder<string, undefined>('test').sendRequest()
await expect(request).rejects.toEqual(new ApiError(401, 'testExplosion', 'The API has exploded!'))
})
})
})