-
Notifications
You must be signed in to change notification settings - Fork 4
fix: change custom error handler to display message from API #906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |||||||||||||||||||||||
| import T from "i18n-react/dist/i18n-react"; | ||||||||||||||||||||||||
| import pLimit from "p-limit"; | ||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||
| VALIDATE, | ||||||||||||||||||||||||
| authErrorHandler, | ||||||||||||||||||||||||
| createAction, | ||||||||||||||||||||||||
| deleteRequest, | ||||||||||||||||||||||||
|
|
@@ -113,11 +114,24 @@ export const customErrorHandler = (ticketId, err, res) => (dispatch) => { | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| switch (code) { | ||||||||||||||||||||||||
| case ERROR_CODE_412: | ||||||||||||||||||||||||
| Swal.fire( | ||||||||||||||||||||||||
| "Validation error", | ||||||||||||||||||||||||
| `Ticket number ${ticketId} not found.`, | ||||||||||||||||||||||||
| "warning" | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| if (Array.isArray(err.response.body)) { | ||||||||||||||||||||||||
| err.response.body.forEach((er) => { | ||||||||||||||||||||||||
| msg += `${er}<br>`; | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| Object.keys(err.response.body).forEach((key) => { | ||||||||||||||||||||||||
| msg += `${err.response.body[key]}<br>`; | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Swal.fire("Validation error", msg, "warning"); | ||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🌐 Web query:
💡 Result: Yes. In SweetAlert2, the shorthand call Swal.fire(title, html, icon) maps the second argument to the html parameter (i.e., the modal body is treated as HTML). No. SweetAlert2 does not sanitize the html parameter by default. Its TypeScript definitions explicitly state that it “does NOT sanitize this parameter” and that escaping user input is the developer’s responsibility to prevent XSS. So, if you pass user-controlled strings as the second argument, you must escape/sanitize them yourself before calling Swal.fire(...). Citations:
🏁 Script executed: cat -n src/actions/ticket-actions.js | sed -n '110,145p'Repository: fntechgit/summit-admin Length of output: 1252 🏁 Script executed: cat -n src/actions/ticket-actions.js | sed -n '100,135p'Repository: fntechgit/summit-admin Length of output: 1429 🏁 Script executed: cat -n src/actions/ticket-actions.js | sed -n '80,130p'Repository: fntechgit/summit-admin Length of output: 2545 🏁 Script executed: cat -n src/actions/ticket-actions.js | sed -n '1,130p'Repository: fntechgit/summit-admin Length of output: 5937 🏁 Script executed: rg "customErrorHandler" -B5 -A50 src/actions/ticket-actions.js | head -80Repository: fntechgit/summit-admin Length of output: 2686 🏁 Script executed: rg -B10 "msg +\+=" src/actions/ticket-actions.jsRepository: fntechgit/summit-admin Length of output: 495 🏁 Script executed: rg "msg\s*=" src/actions/ticket-actions.js | head -20Repository: fntechgit/summit-admin Length of output: 48 🏁 Script executed: rg "customErrorHandler" -A60 src/actions/ticket-actions.js | head -100Repository: fntechgit/summit-admin Length of output: 3157 🏁 Script executed: rg "let msg|const msg|var msg" src/actions/ticket-actions.jsRepository: fntechgit/summit-admin Length of output: 48 🏁 Script executed: rg "msg" src/actions/ticket-actions.js -B2 -A2Repository: fntechgit/summit-admin Length of output: 434 🏁 Script executed: rg "^(let|const|var)\s+msg|^msg\s*=" src/actions/ticket-actions.jsRepository: fntechgit/summit-admin Length of output: 48 🏁 Script executed: cat src/actions/ticket-actions.js | grep -n "msg"Repository: fntechgit/summit-admin Length of output: 211 🏁 Script executed: sed -n '110,140p' src/actions/ticket-actions.js | cat -nRepository: fntechgit/summit-admin Length of output: 1016 🏁 Script executed: head -150 src/actions/ticket-actions.js | tail -100Repository: fntechgit/summit-admin Length of output: 4315 🏁 Script executed: wc -l src/actions/ticket-actions.jsRepository: fntechgit/summit-admin Length of output: 101 🏁 Script executed: rg "msg\s*=" -B20 src/actions/ticket-actions.js | grep -A20 -B20 "msg"Repository: fntechgit/summit-admin Length of output: 48 Use the Line 127 passes API-derived content directly into SweetAlert2's HTML parameter without sanitization. SweetAlert2 does not sanitize the Recommended fix- Swal.fire("Validation error", msg, "warning");
+ Swal.fire({
+ title: "Validation error",
+ text: msg.replace(/<br>/g, "\n"),
+ icon: "warning"
+ });📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (err.response.body.errors) { | ||||||||||||||||||||||||
| dispatch({ | ||||||||||||||||||||||||
| type: VALIDATE, | ||||||||||||||||||||||||
| payload: { errors: err.response.body } | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
Comment on lines
+129
to
+133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You check 🧩 Proposed fix- if (err.response.body.errors) {
+ if (err.response.body.errors) {
dispatch({
type: VALIDATE,
- payload: { errors: err.response.body }
+ payload: { errors: err.response.body.errors }
});
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||
| dispatch(authErrorHandler(err, res)); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: fntechgit/summit-admin
Length of output: 1187
msgis undeclared and will crash at runtime.Lines 119 and 123 use
msgwithout declaring it. This causes a ReferenceError when the ERROR_CODE_412 handler runs. Guarderr.response.bodybefore accessing it.🔧 Proposed fix
switch (code) { - case ERROR_CODE_412: - if (Array.isArray(err.response.body)) { - err.response.body.forEach((er) => { + case ERROR_CODE_412: { + const body = err?.response?.body; + if (!body) { + Swal.fire("Validation error", "", "warning"); + break; + } + + let msg = ""; + if (Array.isArray(body)) { + body.forEach((er) => { msg += `${er}<br>`; }); } else { - Object.keys(err.response.body).forEach((key) => { - msg += `${err.response.body[key]}<br>`; + Object.keys(body).forEach((key) => { + msg += `${body[key]}<br>`; }); } Swal.fire("Validation error", msg, "warning"); + if (body.errors) { + dispatch({ + type: VALIDATE, + payload: { errors: body.errors } + }); + } + break; + } - - if (err.response.body.errors) { - dispatch({ - type: VALIDATE, - payload: { errors: err.response.body } - }); - } - break;🤖 Prompt for AI Agents