Skip to content

Commit

Permalink
feat:  error handling exploration
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Jul 19, 2024
1 parent ca94535 commit fbd9d1a
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 24 deletions.
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';
};

0 comments on commit fbd9d1a

Please sign in to comment.