Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3xx codes throw errors #614

Closed
Closed
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
2 changes: 1 addition & 1 deletion src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export class Connection {
) {
res.body = parsedBody;
reject(new ArangoError(res));
} else if (res.statusCode && res.statusCode >= 400) {
} else if (res.statusCode && res.statusCode >= 300) {
res.body = parsedBody;
reject(new HttpError(res));
} else {
Expand Down
13 changes: 11 additions & 2 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import ExtendableError from "./util/error";

const messages: { [key: string]: string } = {
0: "Network Error",
300: "Multiple Choices",
301: "Moved Permanently",
302: "Found",
303: "See Other",
304: "Not Modified",
305: "Use Proxy",
306: "Switch Proxy",
307: "Temporary Redirect",
308: "Permanent Redirect",
400: "Bad Request",
401: "Unauthorized",
402: "Payment Required",
Expand Down Expand Up @@ -76,7 +85,7 @@ export class ArangoError extends ExtendableError {
const err = new Error(this.message);
err.name = this.name;
for (const key of nativeErrorKeys) {
if (err[key]) this[key] = err[key];
if (err[key]) (this as any)[key] = err[key] as string;
}
}
}
Expand All @@ -95,7 +104,7 @@ export class HttpError extends ExtendableError {
const err = new Error(this.message);
err.name = this.name;
for (const key of nativeErrorKeys) {
if (err[key]) this[key] = err[key];
if (err[key]) this[key] = err[key] as string;
}
}
}
25 changes: 25 additions & 0 deletions src/test/00-basics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as http from "http";
import * as https from "https";
import arangojs, { Database } from "../arangojs";
import { Connection } from "../connection";
import { AddressInfo } from "net";

describe("Creating a Database", () => {
describe("using the factory", () => {
Expand Down Expand Up @@ -171,3 +172,27 @@ describe("Configuring the driver", () => {
});
});
});
describe("Error handling", function() {
it("3xx codes should be treated as errors", async function() {
const server = http
.createServer((_req, res) => {
res.writeHead(308, {});
res.write("308 Permanent Redirect");
res.end();
})
.listen();
const conn = new Connection({
url: "http://127.0.0.1:" + (server.address() as AddressInfo).port
});
try {
await conn.request({});
expect.fail("Error should be thrown");
} catch (e) {
if (e.name !== "AssertionError") {
expect(e.message).eq("Permanent Redirect");
}
} finally {
server.close();
}
});
});