Skip to content

Commit

Permalink
fix: include more details in fast fail error message
Browse files Browse the repository at this point in the history
Closes #100

Co-authored-by: steveoh <sgourley@utah.gov>
  • Loading branch information
stdavis and steveoh committed Dec 23, 2021
1 parent 3e1146b commit b6a9728
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
47 changes: 45 additions & 2 deletions src/pages/Geocoding.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function Geocoding() {
activeMatchRate: 0,
averageScore: 0,
status: 'idle',
lastRequest: null,
});
const startTime = useRef(new Date());
const { geocodeContext } = useGeocodeContext();
Expand Down Expand Up @@ -53,6 +54,20 @@ export default function Geocoding() {
const timePerRow = elapsedTime / stats.rowsProcessed;
const estimatedTimeRemaining = timePerRow * (stats.totalRows - stats.rowsProcessed);

const formatError = (statusCode, body) => {
if (statusCode >= 500) {
return body;
}

try {
const response = JSON.parse(body);

return response.message;
} catch {
return body;
}
};

const getElementsByStatus = (status) => {
switch (status) {
case 'running': {
Expand Down Expand Up @@ -104,9 +119,37 @@ export default function Geocoding() {
<h3 className="text-center text-red-800">This job has fast failed</h3>
<p>
A fast failure occurs when the <span className="font-bold">first {stats.failures} records</span> do not
succeed to geocode. This is often an indication that the data being processed is not correct. You should
try to fix the data and re-run the job.
succeed to geocode. You might need to remove or reorder the first {stats.failures} rows of your data to
avoid fast failing.
</p>
<ol className="ml-8 list-decimal">
<li>
<strong>The fields could be mapped incorrectly.</strong>{' '}
<Link to="/data">Go back to the data page</Link> and make sure the sample data looks correct.
</li>
<li>
<strong>Your API key could be invalid.</strong> <Link to="/?skip-forward=1">Check that your key</Link>{' '}
has a thumbs up on the API page and create a new key if this is incorrect.
</li>
<li>
<strong>The Web API is having trouble.</strong>{' '}
<a href="https://agrc-status.netlify.app" target="_blank" rel="noopener noreferrer">
Check our status page
</a>{' '}
for any reported outages.
</li>
</ol>
<p>
This is the Web API response for the last request (street: {stats.lastRequest?.request.street}, zone:{' '}
{stats.lastRequest?.request.zone}) to help debug the issue
</p>
<pre className="px-3 py-2 mx-6 text-white whitespace-normal bg-red-400 border-red-800 rounded shadow">
<div className="mb-2">{stats.lastRequest.request.url}</div>
<div>
{stats.lastRequest?.response.status} -{' '}
{formatError(stats.lastRequest?.response.status, stats.lastRequest?.response.body)}
</div>
</pre>
<p>
<Link to="/plan">Go back to the plan page</Link> to restart the process.
</p>
Expand Down
39 changes: 38 additions & 1 deletion src/services/geocode.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ export const geocode = async (event, { filePath, fields, apiKey, wkid = 26912, s
let totalScore = 0;
let failures = 0;
const fastFailLimit = 25;
let lastRequest = {
request: {
street: null,
zone: null,
url: null,
},
response: {
status: 0,
body: null,
},
};

for await (const record of parser) {
if (cancelled) {
Expand All @@ -101,6 +112,7 @@ export const geocode = async (event, { filePath, fields, apiKey, wkid = 26912, s
averageScore: Math.round(totalScore / (rowsProcessed - failures)) || 0,
activeMatchRate: (rowsProcessed - failures) / rowsProcessed || 0,
status: cancelled,
lastRequest,
});

return;
Expand All @@ -117,6 +129,18 @@ export const geocode = async (event, { filePath, fields, apiKey, wkid = 26912, s

response = { status: -1 };
} else {
lastRequest = {
request: {
street,
zone,
url: null,
},
response: {
status: 0,
body: null,
},
};

try {
response = await got(`geocode/${street}/${zone}`, {
headers: {
Expand All @@ -131,15 +155,26 @@ export const geocode = async (event, { filePath, fields, apiKey, wkid = 26912, s
prefixUrl: 'https://api.mapserv.utah.gov/api/v1/',
timeout: 5000,
}).json();

lastRequest.response = {
status: response.status,
body: response.body,
};
} catch (error) {
log.error(`Error geocoding ${street} ${zone}: ${error}`);

try {
response = JSON.parse(error.response.body);
} catch (error) {
} catch {
response = { error: error.message };
}

lastRequest.request.url = error?.request?.requestUrl;
lastRequest.response = {
status: error.response.statusCode,
body: response?.error ?? response?.message,
};

failures += 1;
}
}
Expand Down Expand Up @@ -170,6 +205,7 @@ export const geocode = async (event, { filePath, fields, apiKey, wkid = 26912, s
averageScore: Math.round(totalScore / (rowsProcessed - failures)),
activeMatchRate: (rowsProcessed - failures) / rowsProcessed,
status: 'running',
lastRequest,
});

if (failures === fastFailLimit && fastFailLimit === rowsProcessed) {
Expand All @@ -188,6 +224,7 @@ export const geocode = async (event, { filePath, fields, apiKey, wkid = 26912, s
averageScore: Math.round(totalScore / (rowsProcessed - failures)),
activeMatchRate: (rowsProcessed - failures) / rowsProcessed,
status: 'complete',
lastRequest,
});
};

Expand Down
4 changes: 3 additions & 1 deletion src/styles/tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
@apply my-5 text-3xl font-extrabold tracking-tight text-gray-800 select-none;
}
p,
summary {
summary,
li,
pre {
@apply mb-6 text-lg font-medium text-gray-800;
}
label {
Expand Down

0 comments on commit b6a9728

Please sign in to comment.