refactor: More precise error messages

This commit is contained in:
Ruben Verborgh 2020-09-04 23:15:35 +02:00
parent 389fb33334
commit 063437e5c1
2 changed files with 18 additions and 20 deletions

View File

@ -12,31 +12,29 @@ import { PermissionsExtractor } from './PermissionsExtractor';
* while DELETEs require write permissions as well. * while DELETEs require write permissions as well.
*/ */
export class SparqlPatchPermissionsExtractor extends PermissionsExtractor { export class SparqlPatchPermissionsExtractor extends PermissionsExtractor {
public async canHandle(input: Operation): Promise<void> { public async canHandle({ method, body }: Operation): Promise<void> {
if (input.method !== 'PATCH') { if (method !== 'PATCH') {
throw new UnsupportedHttpError('Only PATCH operations are supported.'); throw new UnsupportedHttpError(`Cannot determine permissions of ${method}, only PATCH.`);
} }
if (!input.body) { if (!body) {
throw new UnsupportedHttpError('PATCH body is required to determine permissions.'); throw new UnsupportedHttpError('Cannot determine permissions of PATCH operations without a body.');
} }
if (!this.isSparql(input.body)) { if (!this.isSparql(body)) {
throw new UnsupportedHttpError('Only SPARQL update PATCHes are supported.'); throw new UnsupportedHttpError('Cannot determine permissions of non-SPARQL patches.');
} }
if (!this.isDeleteInsert(input.body.algebra)) { if (!this.isDeleteInsert(body.algebra)) {
throw new UnsupportedHttpError('Only DELETE/INSERT SPARQL update operations are supported.'); throw new UnsupportedHttpError('Cannot determine permissions of a PATCH without DELETE/INSERT.');
} }
} }
public async handle(input: Operation): Promise<PermissionSet> { public async handle({ body }: Operation): Promise<PermissionSet> {
// Verified in `canHandle` call // Verified in `canHandle` call
const op = (input.body as SparqlUpdatePatch).algebra as Algebra.DeleteInsert; const update = (body as SparqlUpdatePatch).algebra as Algebra.DeleteInsert;
const read = false;
const write = this.needsWrite(op);
// Since `append` is a specific type of write, it is true if `write` is true. // Since `append` is a specific type of write, it is true if `write` is true.
const append = write || this.needsAppend(op); const read = false;
const write = this.needsWrite(update);
const append = write || this.needsAppend(update);
return { read, write, append }; return { read, write, append };
} }

View File

@ -12,14 +12,14 @@ describe('A SparqlPatchPermissionsExtractor', (): void => {
const operation = { method: 'PATCH', body: { algebra: factory.createDeleteInsert() }} as unknown as Operation; const operation = { method: 'PATCH', body: { algebra: factory.createDeleteInsert() }} as unknown as Operation;
await expect(extractor.canHandle(operation)).resolves.toBeUndefined(); await expect(extractor.canHandle(operation)).resolves.toBeUndefined();
await expect(extractor.canHandle({ ...operation, method: 'GET' })) await expect(extractor.canHandle({ ...operation, method: 'GET' }))
.rejects.toThrow(new UnsupportedHttpError('Only PATCH operations are supported.')); .rejects.toThrow(new UnsupportedHttpError('Cannot determine permissions of GET, only PATCH.'));
await expect(extractor.canHandle({ ...operation, body: undefined })) await expect(extractor.canHandle({ ...operation, body: undefined }))
.rejects.toThrow(new UnsupportedHttpError('PATCH body is required to determine permissions.')); .rejects.toThrow(new UnsupportedHttpError('Cannot determine permissions of PATCH operations without a body.'));
await expect(extractor.canHandle({ ...operation, body: {} as SparqlUpdatePatch })) await expect(extractor.canHandle({ ...operation, body: {} as SparqlUpdatePatch }))
.rejects.toThrow(new UnsupportedHttpError('Only SPARQL update PATCHes are supported.')); .rejects.toThrow(new UnsupportedHttpError('Cannot determine permissions of non-SPARQL patches.'));
await expect(extractor.canHandle({ ...operation, await expect(extractor.canHandle({ ...operation,
body: { algebra: factory.createMove('DEFAULT', 'DEFAULT') } as unknown as SparqlUpdatePatch })) body: { algebra: factory.createMove('DEFAULT', 'DEFAULT') } as unknown as SparqlUpdatePatch }))
.rejects.toThrow(new UnsupportedHttpError('Only DELETE/INSERT SPARQL update operations are supported.')); .rejects.toThrow(new UnsupportedHttpError('Cannot determine permissions of a PATCH without DELETE/INSERT.'));
}); });
it('requires append for INSERT operations.', async(): Promise<void> => { it('requires append for INSERT operations.', async(): Promise<void> => {