title: How to return error message from service to controller and then to client
body: <p>How can I return error message from service.cs to controller and then to client side (Angular) when element is null for example?
  What is the correct way to return information what goes wrong on server side? I just want ot return ex.Message or string with information when for example element is null.</p>
  <p><strong>Angular</strong></p>
  <pre><code> getTree(id: number) {
      return this.http.get&lt;any&gt;(this.baseURL + 'home/Tree/' + id)
        .pipe(
          retry(0),
          catchError(this.errorHandler)
        );
    }
  
  
  
  
  errorHandler(error) {
      let errorMessage = '';
      if (error.error instanceof ErrorEvent) {
        // Get client-side error
        errorMessage = error.error.message;
      } else {
        // Get server-side error
        errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
      }
      console.log(errorMessage);
      return throwError(errorMessage);
    }
  </code></pre>
  <p><strong>Controller</strong></p>
  <pre><code> [HttpGet]
   [Route(&quot;Tree/{elementId}&quot;)]
   public FoodNode Tree(int elementId)
      {
          try
          {
              var json = _service.GetConnectionsForId(elementId);
  
              return json;
          }
          catch (Exception ex)
          {
  
              throw;
          }
      }
  </code></pre>
  <p><strong>Service.cs</strong></p>
  <pre><code>  public ElementNode GetConnectionsForId(int id)
      {
          try
          {
              string message;
  
              Element element = _context.Elements.Find(id);
           }
           catch(Exception ex){
             return ex.Message;
           }
  </code></pre>