Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
import Client, { bootstrap } from "./src/client.js";
import {
ImplementationError,
LibraryError,
MissingContentTypeError,
RequestError,
ResponseError,
ServerError,
UnexpectedContentError,
UnexpectedContentTypeError,
UnprocessableResponseError,
UsageError,
} from "./src/errors.js";

export default Client;
export { bootstrap };
export {
ImplementationError,
LibraryError,
MissingContentTypeError,
RequestError,
ResponseError,
ServerError,
UnexpectedContentError,
UnexpectedContentTypeError,
UnprocessableResponseError,
UsageError,
};
22 changes: 14 additions & 8 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ImplementationError,
LibraryError,
MissingContentTypeError,
RequestError,
ServerError,
Expand Down Expand Up @@ -122,7 +123,7 @@ export default function Client(initialURL) {
return;
}
lastResource = resource || lastResource;
lastProblem = problem;
lastProblem = problem instanceof LibraryError ? problem.detail : problem;
lastURL = url || lastURL;
send();
};
Expand Down Expand Up @@ -200,7 +201,6 @@ export default function Client(initialURL) {
mimeType.startsWith("application/problem+json"))
) {
const problem = new UnexpectedContentTypeError(
response,
`the server responded in with an unrecognizable media type: ${mimeType}`,
{ response },
);
Expand Down Expand Up @@ -251,25 +251,31 @@ export default function Client(initialURL) {
update(id, { resource, url });
return true;
}
const errorDetails = (doc.errors || []).filter((e) => e.detail).map((e) =>
`detail: ${e.detail}`
console.assert(
mimeType.startsWith("application/vnd.api+json") ||
mimeType.startsWith("application/problem+json"),
);
const errorDetail = mimeType.startsWith("application/problem+json")
? doc.detail
: (doc.errors || []).filter((e) => e.detail).map((e) =>
`detail: ${e.detail}`
).join(", ");
if (response.status >= 400 && response.status <= 499) {
const problem = new RequestError(
[
"request error",
`${response.status} ${response.statusText}`,
...errorDetails,
errorDetail,
].join(": "),
{ doc, response },
{ response },
);
update(id, { problem, url });
} else if (response.status >= 500 && response.status <= 599) {
const problem = new ServerError(response, [
"response error",
`${response.status} ${response.statusText}`,
...errorDetails,
], { doc, response });
errorDetail,
], { response });
update(id, { problem, url });
} else {
throw new ImplementationError(
Expand Down
12 changes: 8 additions & 4 deletions src/client_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import Client, { isLocalURL } from "./client.js";
import {
assert,
assertEquals,
assertInstanceOf,
} from "https://deno.land/std@0.185.0/testing/asserts.ts";
import TestServer from "./internal/testing/server.js";
import { UnexpectedContentTypeError } from "./errors.js";

Deno.test("Client", async (t) => {
const serverOptions = { hostname: "0.0.0.0", port: 3003 };
Expand Down Expand Up @@ -135,7 +133,10 @@ Deno.test("Client", async (t) => {
let { status } = client.response();
assertEquals(status, 200);
assertEquals(resource, undefined);
assertInstanceOf(problem, UnexpectedContentTypeError);
assertEquals(
problem.type,
"https://docs.applura.com/client/v2/errors#UnexpectedContentTypeError",
);
// Get a good response.
server.respondWith(
new Response(
Expand Down Expand Up @@ -163,7 +164,10 @@ Deno.test("Client", async (t) => {
({ status } = client.response());
assertEquals(status, 200);
assertEquals(resource.id, "200 resource");
assertInstanceOf(problem, UnexpectedContentTypeError);
assertEquals(
problem.type,
"https://docs.applura.com/client/v2/errors#UnexpectedContentTypeError",
);
client.stop();
});
});
Expand Down
37 changes: 28 additions & 9 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ export class LibraryError extends Error {
super(message, options);
this.name = "LibraryError";
}
get detail() {
return {
type: `https://docs.applura.com/client/v2/errors#${this.name}`,
title: this.name,
detail: this.message,
};
}
}

// Raised when the implementation of this library has caused an error. For example, when a known edge case has not been
// handled.
export class ImplementationError extends Error {
export class ImplementationError extends LibraryError {
constructor(message, options) {
super(message, options);
this.name = "ImplementationError";
Expand All @@ -23,22 +30,34 @@ export class UsageError extends LibraryError {
}
}

// Raised when an HTTP request causes an HTTP client error, i.e. for HTTP status codes >=300 and <=399.
export class RequestError extends LibraryError {
constructor(message, { doc, response, ...options }) {
// Raised when an HTTP response is in error, i.e. for HTTP status codes >=400.
export class HTTPError extends LibraryError {
constructor(message, { response, ...options }) {
super(message, options);
this.name = "RequestError";
Object.defineProperty(this, "doc", { value: doc });
Object.defineProperty(this, "response", { value: response });
}
get detail() {
return {
...super.detail,
status: this.response.status,
};
}
}

// Raised when an HTTP request causes an HTTP client error, i.e. for HTTP status codes >=400 and <=499.
export class RequestError extends HTTPError {
constructor(message, options) {
super(message, options);
this.name = "RequestError";
}
}

// Raised when an HTTP response causes an error.
export class ResponseError extends LibraryError {
constructor(message, { doc, response, ...options }) {
super(message, { ...options, doc: { value: doc } });
export class ResponseError extends HTTPError {
constructor(message, options) {
super(message, options);
this.name = "ResponseError";
Object.defineProperty(this, "response", { value: response });
}
}

Expand Down