Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Andrew Elans
Andrew Elans

Posted on

     

Power Automate: format JavaScript error object for email

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 JavaScriptcatch 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(newError('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"}]}}))
Enter fullscreen modeExit fullscreen mode

We would get the following error that we would need to send by email:

Image description

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"}]})})})
Enter fullscreen modeExit fullscreen mode

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"}]}
Enter fullscreen modeExit fullscreen mode

That is not what I want.

How to fix

We need to use thespace parameter when doingJSON.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,'&emsp;'// 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})})
Enter fullscreen modeExit fullscreen mode

Space characters that work in Outlook:

  • Em Space&emsp; - widest of all, but when pasted from outlook into vscode will be replaced with a square symbol invalidating json
  • No-Break Space&#160; - narrow but when pasting into vs code will be replaced with a normal space making valid json ->I will finally use this for production with some modification:

    • I would like to use 4 spaces instead of 1 (my typical setting in the code editor), but since thefetch space param has a limit of 10 characters, I cannot use&#160;&#160;&#160;&#160;
    • instead, I will replace it after
    • so the above line.replaceAll('\n', '<br>') will be replaced with.replaceAll('\n', '<br>').replaceAll('&#160;', '&#160;&#160;&#160;&#160;')
    • as a result I will have 4 spaces rendered and when pasting the json from the email body into vs code (in case I need it), I don't even need to format
  • some other spaces

Space characters that do not work in Outlook:

  • Zero Width Space&#8203;
  • Space (SP)&#32;
  • Tab&#9;

Result

Actual string that will be sent to the email body will look like this:

'{"wb_data":"{<br>&emsp;\\"desc\\":\\"some description here\\",<br>&emsp;\\"comments\\":\\"some comments here\\",<br>&emsp;\\"someObj\\": {<br>&emsp;&emsp;\\"newProp\\":\\"new value\\"<br>&emsp;},<br>&emsp;\\"someArr\\": [<br>&emsp;&emsp;{<br>&emsp;&emsp;&emsp;\\"name\\":\\"name1\\",<br>&emsp;&emsp;&emsp;\\"value\\":\\"value1\\"<br>&emsp;&emsp;},<br>&emsp;&emsp;{<br>&emsp;&emsp;&emsp;\\"name\\":\\"name2\\",<br>&emsp;&emsp;&emsp;\\"value\\":\\"value2\\"<br>&emsp;&emsp;}<br>&emsp;]<br>}"}'
Enter fullscreen modeExit fullscreen mode

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"  }]}
Enter fullscreen modeExit fullscreen mode

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
balagmadhu profile image
Bala Madhusoodhanan
- Curious Learner for Supply chain Domain, Sustainability, Machine Learning, RPA - Fostering innovation and exploring opportunities to drive efficiencies
  • Location
    Aylesbury, United Kingdom
  • Joined

Thanks for sharing

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

This is my sketchbook where I put things I consider relevant to remember in future and share with others.
  • Location
    Oslo, Norway
  • Education
    Teacher of Computer Science 2003 + life-long self-education committment
  • Work
    Digitalizing corporate procurement flows with Microsoft Power Platform and Azure services
  • Joined

More fromAndrew Elans

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp