Correct way to return exception information from main process to renderer process


#1

what is the recommended way to return exception information from main process to renderer process? eg: instead of throwing an exception in the main process below, how should i change the code to handle the exceptions and pass this information to the renderer process?

main.js:
exports.getUrlInformation=(arg)=>
{
var url = arg;
var options = [];
ytdl.getInfo(url, options, function(err, urlInformation)
{
if (err) throw err;
mainWindow.webContents.send(‘UrlInformation’, urlInformation);
});
}

index.js:
var {ipcRenderer, remote} = require(‘electron’);
var mainProcess = remote.require("./main.js");

class YouTubeDownloaderForm extends React.Component 
{
  constructor(props) 
  {
    super(props);
    this.state = 
    {
      url: '', 
      urlsInformation:[]
    };
    this.handleAddClick = this.handleAddClick.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);      
    this.handleChangeClick = this.handleChangeClick.bind(this);
    this.handleUrlChange = this.handleUrlChange.bind(this);
    this.updateUrlInformation = this.updateUrlInformation.bind(this);
  }
  componentDidMount() 
  {
    ipcRenderer.on('UrlInformation', this.updateUrlInformation)
  } 
  componentWillUnmount() 
  {
    ipcRenderer.removeListener('UrlInformation', this.updateUrlInformation)
  }
  updateUrlInformation(event, arg) 
  {
    var urlsInformation = this.state.urlsInformation;
    urlsInformation.push(arg);
    this.setState({urlsInformation: urlsInformation});
  }
  handleAddClick(event) 
  {      
    mainProcess.getUrlInformation(this.state.url);
  }

#2

Please edit your post to mark the code. Either select the relevant text and click the </> button, or fence it like below

```js

<code here>

```

This ensures it can be copied and pasted properly, all whitespace is preserved, and none of it is interpreted as markdown.