Skip to content

Commit

Permalink
Reverts back to errors but with detail
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkSuckerberg committed Mar 12, 2024
1 parent a5a16e9 commit e7c8f49
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/classes/TumblrAPIError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { TumblrAPIResponse } from "../interfaces";

export class TumblrAPIError extends Error {
public response: TumblrAPIResponse;
public queryURL: string;

constructor(message: TumblrAPIResponse, queryURL: string) {
super(`${message.meta.status}: ${message.meta.msg}`);
this.name = "TumblrAPIError";
this.response = message;
this.queryURL = queryURL;
}
}
1 change: 1 addition & 0 deletions src/classes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./TumblrAPIError";
5 changes: 5 additions & 0 deletions src/functions/AccessTumblrApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TumblrAPIError } from "../classes";
import { TumblrAPIResponse } from "../interfaces";

/**
Expand Down Expand Up @@ -35,5 +36,9 @@ export async function accessTumblrAPI(

const response: TumblrAPIResponse = await request.json();

if (!request.ok) {
throw new TumblrAPIError(response, url);
}

return response;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./classes";
export * from "./functions";
export * from "./interfaces";
11 changes: 8 additions & 3 deletions tests/Api.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { accessTumblrAPI } from "../src";
import { accessTumblrAPI, TumblrAPIError } from "../src";

const token = process.env.TUMBLR_TOKEN;

if (!token) throw new Error("No token provided");

it("should error on invalid path", async () => {
const response = await accessTumblrAPI(token, "invalid/path");
expect(response.meta.status).toBe(404);
const error = await accessTumblrAPI(token, "invalid/path").catch<Error>(e => e);
expect(error instanceof TumblrAPIError).toBe(true);
if (error instanceof TumblrAPIError) {
expect(error.response.meta.status).toBe(404);
expect(error.response.meta.msg).toBe("Not Found");
expect(error.queryURL).toBe("https://api.tumblr.com/v2/invalid/path?");
}
});

0 comments on commit e7c8f49

Please sign in to comment.