In my Power Pages portal project I would need to be notified on user client-side errors. I will later make an extensive thread on how I implement this, but this post is how to format a typical error object that comes from the JavaScript catch
handler.
Typical flow
1) Error is caught in JavaScript code
2) Error object is prepared with all client-side details
3) Power Automate flow is triggered where email with the error details is sent to the admin
Error object
Let's now make a dummy Error object:
console.dir(
new Error(
'Smth went wrong'
, {
cause: {
desc: "some descrtion here",
comments: "some comments here",
someObj: {
newProp: "new value"
},
someArr: [
{name: "name1", value: "value1"},
{name: "name2", value: "value2"}
]
}
}
)
)
We would get the following error that we would need to send by email:
We will send this object to Power Automate in the request body as follows:
fetch(
url
, {
method: "POST",
body: JSON.stringify(
{
wb_data: JSON.stringify(
{
desc: "some description here",
comments: "some comments here",
someObj: {
newProp: "new value"
},
someArr: [
{name: "name1", value: "value1"},
{name: "name2", value: "value2"}
]
}
)
}
)
}
)
Result in the email body will be rendered in one line and look like this:
{"desc":"some description here","comments":"some comments here","someObj":{"newProp":"new value"},"someArr":[{"name":"name1","value":"value1"},{"name":"name2","value":"value2"}]}
That is not what I want.
How to fix
We need to use the space parameter when doing JSON.stringify
Let's redo the logic:
fetch(
url
, {
method: "POST",
body: JSON.stringify(
{
wb_data: JSON.stringify(
{
desc: "some description here",
comments: "some comments here",
someObj: {
newProp: "new value"
},
someArr: [
{name: "name1", value: "value1"},
{name: "name2", value: "value2"}
]
}
, null
, ' ' // this is a space separator that is rendered in Outlook HTML tags as a visual space
).replaceAll('\n', '<br>') // when applying the space param, '\n' is added automatically which we need to convert to new line for HTML
}
)
}
)
Ref  
.
Result
Actual string that will be sent to the email body will look like this:
'{"wb_data":"{<br> \\"desc\\": \\"some description here\\",<br> \\"comments\\": \\"some comments here\\",<br> \\"someObj\\": {<br>  \\"newProp\\": \\"new value\\"<br> },<br> \\"someArr\\": [<br>  {<br>   \\"name\\": \\"name1\\",<br>   \\"value\\": \\"value1\\"<br>  },<br>  {<br>   \\"name\\": \\"name2\\",<br>   \\"value\\": \\"value2\\"<br>  }<br> ]<br>}"}'
Actual email body in Outlook will look like this:
{
"desc": "some description here",
"comments": "some comments here",
"someObj": {
"newProp": "new value"
},
"someArr": [
{
"name": "name1",
"value": "value1"
},
{
"name": "name2",
"value": "value2"
}
]
}
Top comments (0)