Skip to content

Commit 085b439

Browse files
committed
feat(toolbar): improve WordPress connection error handling
Enhance error messages in Next.js toolbar example to provide specific, actionable guidance for common WordPress connection failures. Changes: - Add comprehensive status code handling (401, 403, 404, 500+) in wordpress.ts - Parse WordPress error response JSON for detailed error messages - Detect "No route was found" errors and suggest hwp-wp-env-helpers plugin - Provide specific troubleshooting steps for CORS, network, and server errors - Update page.tsx to display detailed error messages instead of generic text Error messages now include: - Multiple possible causes for each error type - Specific commands to resolve issues (npx wp-env start, etc.) - References to required plugins (hwp-cors-local, hwp-wp-env-helpers) - WordPress log checking instructions for server errors
1 parent 1e65d15 commit 085b439

File tree

2 files changed

+81
-5
lines changed

2 files changed

+81
-5
lines changed

examples/next/toolbar-demo/example-app/app/page.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export default function Home() {
3131
console.log('User logged in:', user.name);
3232
} catch (error) {
3333
console.error('Error connecting to WordPress:', error);
34-
alert('Error: Make sure wp-env is running (npx wp-env start)');
34+
const message = error instanceof Error ? error.message : 'Unknown error occurred';
35+
alert(`Error connecting to WordPress:\n\n${message}`);
3536
}
3637
};
3738

@@ -56,7 +57,8 @@ export default function Home() {
5657
}
5758
} catch (error) {
5859
console.error('Error fetching posts:', error);
59-
alert('Error fetching posts. Make sure wp-env is running.');
60+
const message = error instanceof Error ? error.message : 'Unknown error occurred';
61+
alert(`Error fetching posts:\n\n${message}`);
6062
}
6163
};
6264

examples/next/toolbar-demo/example-app/lib/wordpress.ts

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,90 @@ export async function fetchFromWordPress(endpoint: string, options?: RequestInit
99
});
1010

1111
if (!response.ok) {
12+
// Handle CORS errors (status 0 or null)
1213
if (response.status === 0 || !response.status) {
13-
throw new Error('CORS error: Unable to connect to WordPress. Make sure WordPress is running and CORS is configured.');
14+
throw new Error(
15+
`CORS error: WordPress is blocking cross-origin requests.\n\n` +
16+
`Possible causes:\n` +
17+
`• WordPress is not running (run: npx wp-env start)\n` +
18+
`• CORS plugin not active (check hwp-cors-local)\n` +
19+
`• Wrong WordPress URL in .env`
20+
);
1421
}
15-
throw new Error(`WordPress API error: ${response.status} ${response.statusText}`);
22+
23+
// Try to parse WordPress error response
24+
let errorDetails = '';
25+
try {
26+
const errorData = await response.json();
27+
if (errorData.message) {
28+
errorDetails = errorData.message;
29+
}
30+
if (errorData.code) {
31+
errorDetails += ` (${errorData.code})`;
32+
}
33+
} catch {
34+
// Response body not JSON, use status text
35+
errorDetails = response.statusText;
36+
}
37+
38+
// Handle specific status codes
39+
if (response.status === 401) {
40+
throw new Error(
41+
`Authentication required (401).\n\n` +
42+
`This endpoint requires authentication. ${errorDetails ? `\nDetails: ${errorDetails}` : ''}`
43+
);
44+
}
45+
46+
if (response.status === 403) {
47+
throw new Error(
48+
`Permission denied (403).\n\n` +
49+
`You don't have permission to access this resource. ${errorDetails ? `\nDetails: ${errorDetails}` : ''}`
50+
);
51+
}
52+
53+
if (response.status === 404) {
54+
const isRestRouteError = errorDetails.toLowerCase().includes('no route');
55+
throw new Error(
56+
`WordPress endpoint not found (404).\n\n` +
57+
(isRestRouteError
58+
? `REST API routing error detected. This usually happens in wp-env when:\n` +
59+
`• hwp-wp-env-helpers plugin is not loaded\n` +
60+
`• Permalink structure conflicts with Docker\n\n` +
61+
`Try restarting wp-env: npx wp-env destroy && npx wp-env start`
62+
: `The endpoint ${endpoint} may not exist or WordPress needs configuration.`) +
63+
(errorDetails ? `\n\nDetails: ${errorDetails}` : '')
64+
);
65+
}
66+
67+
if (response.status >= 500) {
68+
throw new Error(
69+
`WordPress server error (${response.status}).\n\n` +
70+
`Possible causes:\n` +
71+
`• WordPress fatal error or plugin conflict\n` +
72+
`• Database connection issues\n` +
73+
`• Check WordPress logs: npx wp-env run cli wp debug.log` +
74+
(errorDetails ? `\n\nDetails: ${errorDetails}` : '')
75+
);
76+
}
77+
78+
// Generic error for other status codes
79+
throw new Error(
80+
`WordPress API error (${response.status})${errorDetails ? `\n\nDetails: ${errorDetails}` : ''}`
81+
);
1682
}
1783

1884
return response.json();
1985
} catch (error) {
86+
// Network/connection errors
2087
if (error instanceof TypeError && error.message.includes('fetch')) {
21-
throw new Error(`Cannot connect to WordPress at ${WP_API_URL}. Is wp-env running?`);
88+
throw new Error(
89+
`Cannot connect to WordPress at ${WP_API_URL}\n\n` +
90+
`Possible causes:\n` +
91+
`• WordPress is not running (run: npx wp-env start)\n` +
92+
`• Wrong URL in .env file\n` +
93+
`• Network/firewall blocking connection\n\n` +
94+
`Check if WordPress is accessible: ${WP_API_URL}`
95+
);
2296
}
2397
throw error;
2498
}

0 commit comments

Comments
 (0)