Move pagination error to AstroErrorData (#6149)

* Move error to AstroErrorData

* Add changeset
This commit is contained in:
Andrew Bloyce 2023-02-07 02:30:36 +10:00 committed by GitHub
parent 6a59531ff9
commit 592386b755
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Moved pagination error to AstroErrorData

View file

@ -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.',

View file

@ -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(
@ -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));