Skip to content

Commit

Permalink
refactor: Result error type (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
SofianD authored Mar 15, 2024
1 parent ccc4349 commit 254b32b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
12 changes: 5 additions & 7 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@ import * as Utils from "./utils";
import { computeURI } from "./agents";
import { HttpieResponseHandler, ModeOfHttpieResponseHandler } from "./class/undiciResponseHandler";
import { HttpieOnHttpError } from "./class/HttpieOnHttpError";
import { HttpieError } from "./class/HttpieCommonError";
import { HttpieDecompressionError, HttpieFetchBodyError, HttpieParserError } from "./class/HttpieHandlerError";

export type WebDavMethod = "MKCOL" | "COPY" | "MOVE" | "LOCK" | "UNLOCK" | "PROPFIND" | "PROPPATCH";
export type HttpMethod = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH" ;
export type InlineCallbackAction = <T>(fn: () => Promise<T>) => Promise<T>;

export type RequestError<T> =
Partial<HttpieOnHttpError<RequestResponse<T>>> &
Partial<HttpieDecompressionError> &
Partial<HttpieFetchBodyError> &
Partial<HttpieParserError> &
HttpieError;
export type RequestError<T> =
HttpieOnHttpError<RequestResponse<T>> |
HttpieDecompressionError |
HttpieFetchBodyError |
HttpieParserError;

export interface RequestOptions {
/** @default 0 */
Expand Down
13 changes: 9 additions & 4 deletions test/request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FastifyInstance } from "fastify";

// Import Internal Dependencies
import { get, post, put, patch, del, safeGet } from "../src/index";
import { isHTTPError } from "../src/utils";

// Helpers and mock
import { createServer } from "./server/index";
Expand Down Expand Up @@ -177,16 +178,20 @@ describe("http.safeGet", () => {
});

it("should throw a 404 Not Found error because the path is not known", async() => {
expect.assertions(5);

const result = await safeGet<string, any>("/windev/hlkezcjcke");
expect(result.err).toStrictEqual(true);

if (result.err) {
const error = result.val;

expect(error.name).toStrictEqual("HttpieOnHttpError");
expect(error.statusCode).toStrictEqual(404);
expect(error.statusMessage).toStrictEqual("Not Found");
expect(error.data).toMatchSnapshot();
if (isHTTPError(error)) {
expect(error.name).toStrictEqual("HttpieOnHttpError");
expect(error.statusCode).toStrictEqual(404);
expect(error.statusMessage).toStrictEqual("Not Found");
expect(error.data).toMatchSnapshot();
}
}
});
});

0 comments on commit 254b32b

Please sign in to comment.