Skip to content

Commit

Permalink
Show axios error reason with JSON
Browse files Browse the repository at this point in the history
I didn't notice until trying to debug some Axios responses, but for some
reason, axios doesn't include `error.response.data`, the attribute that
says why the API call failed (on docassemble at least) in `error.toJSON()`.

This change adds it back in the returned JS object manually, additionally
optional chaining to make sure that it doesn't crash if `error.response`
isn't present for some reason. Optional chaining is supported in Node 14
and above, which we are on.
  • Loading branch information
BryceStevenWilley committed Mar 21, 2023
1 parent 038fc84 commit 1ded185
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/docassemble/docassemble_api_REST.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ da.get_dev_id = async function ( timeout ) {
try {
return await axios.request( options );
} catch (error) {
throw error.toJSON();
throw {...error.toJSON(), data: error.response?.data};
}
}; // Ends da.get_dev_id()

Expand All @@ -48,7 +48,7 @@ da.create_project = async function ( project_name, timeout ) {
try {
return await axios.request( options );
} catch (error) {
throw error.toJSON();
throw {...error.toJSON(), data: error.response?.data};
}
}; // Ends da.create_project()

Expand All @@ -71,7 +71,7 @@ da.pull = async function ( timeout ) {
try {
return await axios.request( options );
} catch (error) {
throw error.toJSON();
throw {...error.toJSON(), data: error.response?.data};
}
}; // Ends da.pull()

Expand All @@ -92,7 +92,7 @@ da.has_task_finished = async function ( task_id, timeout ) {
try {
return await axios.request( options );
} catch (error) {
throw error.toJSON();
throw {...error.toJSON(), data: error.response?.data};
}
}; // Ends da.has_task_finished()

Expand All @@ -113,7 +113,7 @@ da.delete_project = async function ( timeout ) {
try {
return await axios.request( options );
} catch (error) {
throw error.toJSON();
throw {...error.toJSON(), data: error.response?.data};
}
}; // Ends da.delete_project()

Expand Down Expand Up @@ -142,7 +142,7 @@ da.__delete_projects_starting_with = async function ( base_name, starting_num=1,

await axios.request( options );
} catch ( error ) {
console.log( error.toJSON() )
console.log( {...error.toJSON(), data: error.response?.data});
}
name_incrementor++;
}
Expand Down

0 comments on commit 1ded185

Please sign in to comment.