Skip to content

Commit

Permalink
fix: throw PrismicRepositoryNotFound error when a repository does n…
Browse files Browse the repository at this point in the history
…ot exist
  • Loading branch information
angeloashmore committed Oct 10, 2023
1 parent 66c33d0 commit f719e31
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { PreviewTokenExpiredError } from "./errors/PreviewTokenExpired";
import { PrismicError } from "./errors/PrismicError";
import { RefExpiredError } from "./errors/RefExpiredError";
import { RefNotFoundError } from "./errors/RefNotFoundError";
import { RepositoryNotFoundError } from "./errors/RepositoryNotFoundError";

import { LinkResolverFunction, asLink } from "./helpers/asLink";

Expand Down Expand Up @@ -1912,9 +1913,10 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
// Not Found
// - Incorrect repository name (this response has an empty body)
// - Ref does not exist
// - Preview token is expired
case 404: {
if (res.json === undefined) {
throw new NotFoundError(
throw new RepositoryNotFoundError(
`Prismic repository not found. Check that "${this.endpoint}" is pointing to the correct repository.`,
url,
undefined,
Expand Down
5 changes: 5 additions & 0 deletions src/errors/RepositoryNotFoundError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { NotFoundError } from "./NotFoundError";

export class RepositoryNotFoundError<
TResponse = undefined,
> extends NotFoundError<TResponse> {}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export { RefNotFoundError } from "./errors/RefNotFoundError";
export { RefExpiredError } from "./errors/RefExpiredError";
export { PreviewTokenExpiredError } from "./errors/PreviewTokenExpired";
export { ParsingError } from "./errors/ParsingError";
export { RepositoryNotFoundError } from "./errors/RepositoryNotFoundError";

//=============================================================================
// Types - Types representing Prismic content, models, and API payloads.
Expand Down
6 changes: 4 additions & 2 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ it("throws PrismicError if response is not JSON", async (ctx) => {
await expect(() => client.get()).rejects.toThrowError(prismic.PrismicError);
});

it("throws NotFoundError if repository does not exist", async (ctx) => {
it("throws RepositoryNotFoundError if repository does not exist", async (ctx) => {
const client = createTestClient();

ctx.server.use(
Expand All @@ -681,7 +681,9 @@ it("throws NotFoundError if repository does not exist", async (ctx) => {
await expect(() => client.get()).rejects.toThrowError(
/repository not found/i,
);
await expect(() => client.get()).rejects.toThrowError(prismic.NotFoundError);
await expect(() => client.get()).rejects.toThrowError(
prismic.RepositoryNotFoundError,
);
});

it("throws RefNotFoundError if ref does not exist", async (ctx) => {
Expand Down

0 comments on commit f719e31

Please sign in to comment.