Skip to content

Commit 3d36117

Browse files
committed
Improved content-type handling
1 parent ce0e44f commit 3d36117

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

errors/DefaultErrorHandler.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ErrorHandler, HttpError, BadRequestError, AuthenticationError, AuthorizationError, NotFoundError, TimeoutError, ConflictError, ConcurrencyError, RangeError } from ".";
2-
import { HttpStatusCode } from "../http";
2+
import { HttpStatusCode, HttpHeader } from "../http";
33

44
/**
55
* Handles errors in HTTP responses by mapping status codes to common exception types.
@@ -28,11 +28,9 @@ export class DefaultErrorHandler implements ErrorHandler {
2828
}
2929

3030
private async extractJsonMessage(response: Response) {
31-
try {
32-
return (await response.json()).body;
33-
} catch {
34-
return undefined;
35-
}
31+
const contentType = response.headers.get(HttpHeader.ContentType);
32+
if (contentType?.startsWith("application/json") || contentType?.includes("+json"))
33+
return (await response.json())?.body;
3634
}
3735

3836
private static mapToError(status: HttpStatusCode, message: string) {

links/HalLinkExtractor.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ export class HalLinkExtractor implements LinkExtractor {
99
* @inheritdoc
1010
*/
1111
async getLinks(response: Response): Promise<Link[]> {
12-
switch (response.headers.get(HttpHeader.ContentType)) {
13-
case "application/hal+json":
14-
return this.parseJsonBody(await response.clone().json());
15-
default:
16-
return [];
12+
const contentType = response.headers.get(HttpHeader.ContentType);
13+
if (contentType?.startsWith("application/hal+json")) {
14+
return this.parseJsonBody(await response.clone().json());
15+
} else {
16+
return [];
1717
}
1818
}
1919

0 commit comments

Comments
 (0)