Skip to content

Commit

Permalink
Skip calling endsWith on mediaType when mediaType is undefined
Browse files Browse the repository at this point in the history
This fixes imprecise error messages when mediaType is not recognized.
  • Loading branch information
surilindur committed Sep 11, 2023
1 parent b237be4 commit 5d5fb83
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export abstract class ActorAbstractMediaTypedFixed<HI, HT, HO> extends ActorAbst
this.mediaTypeFormats = Object.freeze(this.mediaTypeFormats);
}

public async testHandle(action: HI, mediaType: string, context: IActionContext): Promise<HT> {
if (!(mediaType in this.mediaTypePriorities)) {
public async testHandle(action: HI, mediaType: string | undefined, context: IActionContext): Promise<HT> {
if (!mediaType || !(mediaType in this.mediaTypePriorities)) {
throw new Error(`Unrecognized media type: ${mediaType}`);
}
return await this.testHandleChecked(action, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ describe('ActorAbstractMediaTypedFixed', () => {
return expect(actor.mediaTypePriorities).toEqual({ a: 0.25 });
});

it('should testParse for a valid media type', () => {
it('should testHandle for a valid media type', () => {
return expect(actor.testHandle(null, 'a')).resolves.toBeTruthy();
});

it('should not testParse for an invalid media type', () => {
it('should not testHandle for an invalid media type', () => {
return expect(actor.testHandle(null, 'b')).rejects.toBeTruthy();
});

it('should not testHandle for an undefined media type', () => {
return expect(actor.testHandle(null)).rejects.toBeTruthy();
});

it('should always testMediaType', () => {
return expect(actor.testMediaType()).resolves.toBeTruthy();
});
Expand Down
5 changes: 3 additions & 2 deletions packages/actor-rdf-parse-jsonld/lib/ActorRdfParseJsonLd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ export class ActorRdfParseJsonLd extends ActorRdfParseFixedMediaTypes {
super(args);
}

public async testHandle(action: IActionRdfParse, mediaType: string, context: IActionContext): Promise<IActorTest> {
public async testHandle(action: IActionRdfParse, mediaType: string | undefined, context: IActionContext):
Promise<IActorTest> {
if (context.has(KeysRdfParseHtmlScript.processingHtmlScript) && mediaType !== 'application/ld+json') {
throw new Error(`JSON-LD in script tags can only have media type 'application/ld+json'`);
}
if (!(mediaType in this.mediaTypePriorities) && !mediaType.endsWith('+json')) {
if (!mediaType || !(mediaType in this.mediaTypePriorities || mediaType.endsWith('+json'))) {
throw new Error(`Unrecognized media type: ${mediaType}`);
}
return await this.testHandleChecked(action);
Expand Down
17 changes: 15 additions & 2 deletions packages/actor-rdf-parse-jsonld/test/ActorRdfParseJsonLd-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,27 @@ describe('ActorRdfParseJsonLd', () => {
it('should not test on N-Triples', async() => {
await expect(actor
.test({ handle: { data: input, context }, handleMediaType: 'application/n-triples', context }))
.rejects.toBeTruthy();
.rejects.toThrow(new Error('Unrecognized media type: application/n-triples'));
await expect(actor
.test({
handle: { data: input, metadata: { baseIRI: '' }, context },
handleMediaType: 'application/n-triples',
context,
}))
.rejects.toBeTruthy();
.rejects.toThrow(new Error('Unrecognized media type: application/n-triples'));
});

it('should not test on undefined', async() => {
await expect(actor
.test({ handle: { data: input, context }, handleMediaType: undefined, context }))
.rejects.toThrow(new Error('Unrecognized media type: undefined'));
await expect(actor
.test({
handle: { data: input, metadata: { baseIRI: '' }, context },
handleMediaType: undefined,
context,
}))
.rejects.toThrow(new Error('Unrecognized media type: undefined'));
});

it('should run', () => {
Expand Down

0 comments on commit 5d5fb83

Please sign in to comment.