Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:  error handling exploration #813

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/next/src/rsc/data/queries/prepareQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import type { NextQueryProps } from './types';

const { all: merge } = deepmerge;

export function prepareQuery<P>(
query: NextQueryProps<P>,
export function prepareQuery<P, Error extends boolean = true>(
query: NextQueryProps<P, Error>,
_config: HeadlessConfig | undefined = undefined,
) {
const { routeParams, handleError = true, ...rest } = query;
Expand All @@ -28,7 +28,7 @@ export function prepareQuery<P>(
);
}

const options = merge<NextQueryProps<P>['options']>([
const options = merge<NextQueryProps<P, Error>['options']>([
{
headers: {
cache: 'no-store',
Expand Down
23 changes: 18 additions & 5 deletions packages/next/src/rsc/data/queries/queryPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,33 @@ import { handleFetchError } from '../handleFetchError';
import { NextQueryProps } from './types';
import { prepareQuery } from './prepareQuery';

type QueryPostsReturnType<T extends PostEntity, P extends PostsArchiveParams> = Awaited<
ReturnType<typeof fetchPosts<T, P>>
>;

export async function queryPosts<
T extends PostEntity = PostEntity,
P extends PostsArchiveParams = PostsArchiveParams,
>(q: NextQueryProps<P> = {}, _config: HeadlessConfig | undefined = undefined) {
const { config, handleError, ...query } = prepareQuery<P>(q, _config);
Error extends boolean = true,
>(
q: NextQueryProps<P, Error> = {},
_config: HeadlessConfig | undefined = undefined,
): Promise<Error extends true ? QueryPostsReturnType<T, P> : Partial<QueryPostsReturnType<T, P>>> {
const { config, handleError, ...query } = prepareQuery<P, Error>(q, _config);

try {
const result = await fetchPosts<T, P>(query, config);

return result;
} catch (error) {
if (error instanceof Error && handleError) {
await handleFetchError(error, config, query.path);
if (handleError) {
if (error instanceof Error) {
await handleFetchError(error, config, query.path);
}
throw error;
}
throw error;

// @ts-expect-error
return {} as unknown as Partial<QueryPostsReturnType<T, P>>;
}
}
4 changes: 2 additions & 2 deletions packages/next/src/rsc/data/queries/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { QueryProps } from '@headstartwp/core';

export type NextQueryProps<P> = {
export type NextQueryProps<P, Error extends boolean = true> = {
routeParams?: {
path?: string | string[];
site?: string;
[k: string]: unknown;
};
handleError?: boolean;
handleError?: Error;
} & Omit<QueryProps<P>, 'path'>;
2 changes: 1 addition & 1 deletion projects/wp-nextjs-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"lint": "next lint --fix"
},
"dependencies": {
"react": "^18",
Expand Down
24 changes: 11 additions & 13 deletions projects/wp-nextjs-app/src/components/Blocks/PostList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ export const PostList: React.FC<PostListProps> = async ({ block }) => {

const { query } = block.attributes;

try {
const {
data: { posts },
} = await queryPosts({
// todo: map the rest of the query object
params: { per_page: query.perPage, postType: query.postType },
// setting handle error to false will disable automatic handling of errors
// i.e you have to handle the error yourself
handleError: false,
});
const { data } = await queryPosts({
// todo: map the rest of the query object
params: { per_page: query.perPage, postType: query.postType },
// setting handle error to false will disable automatic handling of errors
// i.e you have to handle the error yourself
handleError: false,
});

if (data?.posts) {
return (
<>
<h2>Post List</h2>
Expand All @@ -30,15 +28,15 @@ export const PostList: React.FC<PostListProps> = async ({ block }) => {
</pre>

<ul>
{posts.map((post) => (
{data.posts.map((post) => (
<li key={post.id}>
#{post.id} -{post.title.rendered}
</li>
))}
</ul>
</>
);
} catch (e) {
return 'no posts found';
}

return 'no posts found';
};
Loading