Skip to content

Commit

Permalink
Move pagination error to AstroErrorData (#6149)
Browse files Browse the repository at this point in the history
* Move error to AstroErrorData

* Add changeset
  • Loading branch information
bloycey authored Feb 6, 2023
1 parent 6a59531 commit 592386b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-worms-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Moved pagination error to AstroErrorData
13 changes: 13 additions & 0 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,19 @@ See https://docs.astro.build/en/guides/server-side-rendering/ for more informati
message: (name: string) => `Invalid arguments passed to${name ? ` <${name}>` : ''} component.`,
hint: 'Astro components cannot be rendered directly via function call, such as `Component()` or `{items.map(Component)}`.',
},
/**
* @docs
* @see
* - [Pagination](https://docs.astro.build/en/core-concepts/routing/#pagination)
* @description
* The page number parameter was not found in your filepath.
*/
PageNumberParamNotFound: {
title: 'Page number param not found.',
code: 3021,
message: (paramName: string) => `[paginate()] page number param \`${paramName}\` not found in your filepath.`,
hint: 'Rename your file to \`[page].astro\` or \`[...page].astro\`.'
},
// Vite Errors - 4xxx
UnknownViteError: {
title: 'Unknown Vite Error.',
Expand Down
8 changes: 5 additions & 3 deletions packages/astro/src/core/render/paginate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Props,
RouteData,
} from '../../@types/astro';
import { AstroError, AstroErrorData } from '../errors/index.js';

export function generatePaginateFunction(routeMatch: RouteData): PaginateFunction {
return function paginateUtility(
Expand All @@ -23,9 +24,10 @@ export function generatePaginateFunction(routeMatch: RouteData): PaginateFunctio
} else if (routeMatch.params.includes(`${paramName}`)) {
includesFirstPageNumber = true;
} else {
throw new Error(
`[paginate()] page number param \`${paramName}\` not found in your filepath.\nRename your file to \`[page].astro\` or \`[...page].astro\`.`
);
throw new AstroError({
...AstroErrorData.PageNumberParamNotFound,
message: AstroErrorData.PageNumberParamNotFound.message(paramName),
});
}
const lastPage = Math.max(1, Math.ceil(data.length / pageSize));

Expand Down

0 comments on commit 592386b

Please sign in to comment.