Skip to content

Commit ffab136

Browse files
committed
OWC error handling : add debug_info to wp rest response for better error debugging
1 parent 6a6a8d4 commit ffab136

3 files changed

Lines changed: 90 additions & 41 deletions

File tree

includes/class-proxy-datasets-endpoint.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,32 @@ public function fetch_dynamic_datasets( WP_REST_Request $request ) {
7878

7979
// Handle errors in the response.
8080
if ( is_wp_error( $response ) ) {
81-
error_log( 'Fetch error: ' . $response->get_error_message() );
82-
return new WP_Error( 'fetch_error', 'Failed to fetch data', [ 'status' => 500 ] );
81+
$error_message = $response->get_error_message();
82+
error_log( 'Fetch error: ' . $error_message );
83+
84+
return new WP_REST_Response(
85+
[
86+
'error' => true,
87+
'message' => 'Failed to fetch data',
88+
'debug_info' => $error_message,
89+
],
90+
500
91+
);
8392
}
8493

8594
$status_code = wp_remote_retrieve_response_code( $response );
8695
if ( 200 !== $status_code ) {
87-
error_log( 'Unexpected HTTP status code: ' . $status_code );
88-
return new WP_Error( 'http_error', "HTTP request failed with status $status_code", [ 'status' => $status_code ] );
96+
$error_message = "Unexpected HTTP status code: $status_code";
97+
error_log( $error_message );
98+
99+
return new WP_REST_Response(
100+
[
101+
'error' => true,
102+
'message' => "HTTP request failed with status $status_code",
103+
'debug_info' => $error_message,
104+
],
105+
$status_code
106+
);
89107
}
90108

91109
$body = wp_remote_retrieve_body( $response );

src/blocks/owc-openkaarten/streetmap/assets/scripts/edit.js

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ import { useMemo, useLayoutEffect, useState, useEffect } from "@wordpress/elemen
44
import { __ } from "@wordpress/i18n";
55

66
export default function Edit({ attributes, setAttributes }) {
7+
const [errorMessage, setErrorMessage] = useState(null);
78
const [isTypingUrl, setIsTypingUrl] = useState(false);
89
const [datasets, setDatasets] = useState(null);
910
const [selectedDatasets, setSelectedDatasets] = useState(attributes.selected_datasets);
1011
const isValidURL = useMemo(() => {
1112
try {
12-
new URL(attributes.rest_uri);
13-
return true;
13+
const url = new URL(attributes.rest_uri);
14+
return url.protocol === "http:" || url.protocol === "https:" ? true : setErrorMessage(__('Invalid URL', 'openkaarten-frontend-plugin'))
1415
} catch (error) {
16+
setErrorMessage(__('Invalid URL', 'openkaarten-frontend-plugin'))
1517
return false;
1618
}
1719
}, [attributes.rest_uri]);
1820

1921
useEffect(() => {
2022
if (isValidURL) {
23+
setErrorMessage(null)
2124
const { username, password, url } = stripCredentialsFromUrl(attributes.rest_uri);
2225

2326
fetch('/wp-json/openkaarten-frontend-plugin/v1/proxy-datasets', {
@@ -31,36 +34,49 @@ export default function Edit({ attributes, setAttributes }) {
3134
password,
3235
}),
3336
})
34-
.then(response => {
35-
if (!response.ok) {
36-
throw new Error(`Error status: ${response.status}`);
37-
}
38-
return response.json(); // Initial parsing attempt.
39-
})
40-
.then(data => {
41-
42-
// Check if data is a string and might need additional parsing.
43-
if (typeof data === "string") {
44-
try {
45-
data = JSON.parse(data); // Parse again if it's a JSON string.
46-
} catch (error) {
47-
console.error('Error parsing nested JSON:', error);
48-
setDatasets([]);
49-
return;
37+
.then(response => {
38+
if (!response.ok) {
39+
// Try to read the JSON error information.
40+
return response.json()
41+
.then(errData => {
42+
console.error('Server error response:', errData);
43+
setErrorMessage(__(`Fetching datasets failed: ${errData.debug_info || 'Unknown error'}`, 'openkaarten-frontend-plugin'));
44+
throw new Error(errData.message || `HTTP ${response.status}`);
45+
})
46+
.catch(() => {
47+
// Fallback if JSON is not available.
48+
throw new Error(`Error status: ${response.status}`);
49+
});
50+
}
51+
return response.json();
52+
})
53+
.then(data => {
54+
// Check if data is a string and might need additional parsing.
55+
if (typeof data === "string") {
56+
try {
57+
data = JSON.parse(data); // Parse again if it's a JSON string.
58+
} catch (error) {
59+
setErrorMessage(__(`Error parsing nested JSON`, 'openkaarten-frontend-plugin'));
60+
console.error('Error parsing nested JSON:', error);
61+
setDatasets([]);
62+
return;
63+
}
5064
}
51-
}
5265

53-
if (data && data.type === "DatasetCollection" && Array.isArray(data.datasets)) {
54-
setDatasets(data.datasets);
55-
} else {
56-
console.error("Unexpected response format or 'datasets' is not an array.");
57-
setDatasets([]); // Fallback to empty array if structure is unexpected.
58-
}
59-
})
60-
.catch(error => {
61-
console.error('Fetching datasets failed:', error.message);
62-
setDatasets([]);
63-
});
66+
if (data && data.type === "DatasetCollection" && Array.isArray(data.datasets)) {
67+
setErrorMessage(null);
68+
setDatasets(data.datasets);
69+
} else {
70+
setErrorMessage(__("Unexpected response format or 'datasets' is not an array.", 'openkaarten-frontend-plugin'));
71+
console.error("Unexpected response format or 'datasets' is not an array.");
72+
setDatasets([]); // Fallback to empty array if structure is unexpected.
73+
}
74+
})
75+
.catch(error => {
76+
setErrorMessage(__('Fetching datasets failed', 'openkaarten-frontend-plugin'));
77+
console.error('Fetching datasets failed:', error.message);
78+
setDatasets([]);
79+
});
6480
}
6581
}, [attributes.rest_uri, isValidURL]);
6682

@@ -76,7 +92,7 @@ export default function Edit({ attributes, setAttributes }) {
7692

7793
options.unshift({
7894
value: "",
79-
label: __('Select the datalayers you want to display on the map', 'openkaarten-frontend-plugin'),
95+
label: datasets?.length > 0 ? __('Select the datalayers you want to display on the map', 'openkaarten-frontend-plugin') : __('No datalayers found', 'openkaarten-frontend-plugin'),
8096
disabled: true,
8197
});
8298
return options;
@@ -122,10 +138,10 @@ export default function Edit({ attributes, setAttributes }) {
122138
setIsTypingUrl(false);
123139
}}
124140
/>
125-
{!!attributes.rest_uri && !isTypingUrl && !isValidURL && (
126-
<Notice status="error">
141+
{!!attributes.rest_uri && !isTypingUrl && errorMessage && (
142+
<Notice status="error" isDismissible={false}>
127143
<p>
128-
{__('Invalid URL', 'openkaarten-frontend-plugin')}
144+
{errorMessage}
129145
</p>
130146
</Notice>
131147
)}

src/blocks/owc-openkaarten/streetmap/assets/scripts/vue/App.vue

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,17 @@ async function getLocations() {
119119
})
120120
.then((response) => {
121121
if (!response.ok) {
122-
throw new Error(`Proxy error: ${response.status}`);
122+
// Try to read the JSON error information.
123+
return response.json()
124+
.then(errData => {
125+
console.error('Server error response:', errData);
126+
error.value = `Status ${response.status} ${errData.debug_info}`
127+
throw new Error(errData.message || `HTTP ${response.status}`);
128+
})
129+
.catch(() => {
130+
// Fallback if JSON is not available.
131+
throw new Error(`Proxy error: ${response.status}`);
132+
});
123133
}
124134
return response.json();
125135
})
@@ -129,6 +139,7 @@ async function getLocations() {
129139
try {
130140
data = JSON.parse(data); // Parse again if it's a JSON string.
131141
} catch (error) {
142+
console.error('Parsing error', error)
132143
datasets.value = ([]);
133144
return;
134145
}
@@ -138,14 +149,18 @@ async function getLocations() {
138149
datasets.value = data.datasets;
139150
initializeSelectedDatasets(); // Initialize after datasets are loaded
140151
} else {
152+
error.value = "Unexpected response format or 'datasets' is not an array."
141153
console.error("Unexpected response format or 'datasets' is not an array.");
142154
datasets.value = ([]); // Fallback to empty array if structure is unexpected.
143155
}
144156
loading.value = false;
145157
})
146158
.catch((err) => {
147-
console.error("Error in proxy response:", err);
148-
error.value = err.message;
159+
if (err.response && err.response.status === 500) {
160+
error.value = 'The server might be having issues, please try again later';
161+
} else if (err.message.includes('500')) {
162+
error.value = 'The server is busy at the moment, please try again later';
163+
}
149164
loading.value = false;
150165
});
151166
} else {

0 commit comments

Comments
 (0)