diff --git a/src/connection.ts b/src/connection.ts index 1e97eff4f..1d2e0de1b 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -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 { diff --git a/src/error.ts b/src/error.ts index 2dda6aedd..bc2092592 100644 --- a/src/error.ts +++ b/src/error.ts @@ -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", @@ -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; } } } @@ -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; } } } diff --git a/src/test/00-basics.ts b/src/test/00-basics.ts index 76b81683b..bfa0a76fe 100644 --- a/src/test/00-basics.ts +++ b/src/test/00-basics.ts @@ -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", () => { @@ -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(); + } + }); +});