title: a pure synchronous function in javascript that returns an error
body: <p>I have a pure function that looks like :</p>
  
  <pre><code>function foobar(str) {
     let newStr;
     // code that depends on str and forge a new string
     ...
  
     return newStr;
  }
  </code></pre>
  
  <p>Now, I would like to raise an error if <code>str</code> is not as expected.<br>
  My error will be a simple object like { code: 'foo', msg: 'bar' } and I'm searching for the proper way to return this error from my function.</p>
  
  <p>I'd like to avoid exceptions and promises (as they are asynchronous) and ideally, I'd like this function to adopt a functional programming style.<br>
  Any clue?</p>