Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit e1b0d81

Browse files
committed
Add error messages
1 parent 9a18c4b commit e1b0d81

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

api/wordpress/_global/getPostTypeById.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {isHierarchicalPostType} from './postTypes'
1010
* @param {string} postType WP post type.
1111
* @param {Number|string} id Post identifier.
1212
* @param {string} idType Type of ID.
13-
* @return {?Object} Post data or null.
13+
* @return {Object} Post data or error object.
1414
*/
1515
export default async function getPostTypeById(postType, id, idType = 'SLUG') {
1616
// Define single post query based on post type.
@@ -28,20 +28,34 @@ export default async function getPostTypeById(postType, id, idType = 'SLUG') {
2828
// Retrieve post type query.
2929
const query = postTypeQuery?.[postType] ?? null
3030

31-
// If no query is set for given post type, return.
31+
// If no query is set for given post type, return error message.
3232
if (!query) {
33-
return null
33+
return {
34+
isError: true,
35+
message: `Post type \`${postType}\` is not supported.`
36+
}
3437
}
3538

3639
// Get/create Apollo instance.
3740
const apolloClient = initializeApollo()
3841

3942
// Execute query.
40-
const post = await apolloClient.query({query, variables: {id, idType}})
43+
const post = await apolloClient
44+
.query({query, variables: {id, idType}})
45+
.then((post) => post?.data?.[postType] ?? null)
46+
.catch((error) => {
47+
return {
48+
isError: true,
49+
message: error.message
50+
}
51+
})
4152

4253
if (!post) {
43-
return null
54+
return {
55+
isError: true,
56+
message: `An error occurred while trying to retrieve data for ${postType} "${id}."`
57+
}
4458
}
4559

46-
return post?.data?.[postType] ?? null
60+
return post
4761
}

0 commit comments

Comments
 (0)