# Kibana plugin handling errors returned by the API

**URL:** <https://discuss.elastic.co/t/kibana-plugin-handling-errors-returned-by-the-api/255019>\
**Category:** Kibana\
**Tags:** kibana-plugin-development\
**Created:** [November 11, 2020, 7:59am UTC](https://discuss.elastic.co/t/kibana-plugin-handling-errors-returned-by-the-api/255019 "2020-11-11T07:59:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![16318a22907f3cbfa04b](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/16318a22907f3cbfa04b/32/36875_2.png) [@16318a22907f3cbfa04b](https://discuss.elastic.co/u/16318a22907f3cbfa04b)\
**Post date:** [November 11, 2020, 7:59am UTC](https://discuss.elastic.co/t/kibana-plugin-handling-errors-returned-by-the-api/255019/1 "2020-11-11T07:59:27Z")

</div>

I'm trying to display an error message in my kibana plugin. I do it over `http.get(...).then (..).catch( e=>{console.log(e)})`. In the browser console, I see, for example, `error: bad request` and nothing else. I expected to see an object `{statusCode: 400, error: "Bad request", message: "some error description"}`. At the same time, in the console, when I view the network connection log, I see fetch to my API and see a response in the format `{statusCode: 400, error: "Bad request", message: "some error description"}`. Where did message go?

---

<div class="post-metadata">

**Author:** ![Pierre\_Gayvallet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pierre_gayvallet/32/77239_2.png) [@Pierre\_Gayvallet](https://discuss.elastic.co/u/Pierre_Gayvallet)\
**Post date:** [November 11, 2020, 10:26am UTC](https://discuss.elastic.co/t/kibana-plugin-handling-errors-returned-by-the-api/255019/2 "2020-11-11T10:26:30Z")

</div>

`console.log` is kinda stupid when logging errors, and will often only display `e.msg` or `e.toString()`. Which is very misleading.

In case of server-side returned error, The error payload is accessible via `error.body`

```auto
try { 
   await http.fetch(... ) 
} catch(e) {
  console.log(e.body)
}

```

`e.body` will contains the payload you see in the network tab (`{statusCode: 400, error: "Bad request", message: "some error description"}.`)

---

<div class="post-metadata">

**Author:** ![16318a22907f3cbfa04b](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/16318a22907f3cbfa04b/32/36875_2.png) [@16318a22907f3cbfa04b](https://discuss.elastic.co/u/16318a22907f3cbfa04b)\
**Post date:** [November 11, 2020, 11:30am UTC](https://discuss.elastic.co/t/kibana-plugin-handling-errors-returned-by-the-api/255019/3 "2020-11-11T11:30:44Z")

</div>

Thank you! It,s magic.  
Now I have on server side

```auto
return response.badRequest({
          body: 'My error description message'
        })  

```

on front (UI) side I have

```auto
.catch ( e => {
console.log(e.body)
// my code working with error
});

```

and I got

```auto
{statusCode: 400, error: "Bad Request", message: "My error description message"}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 9, 2020, 11:30am UTC](https://discuss.elastic.co/t/kibana-plugin-handling-errors-returned-by-the-api/255019/4 "2020-12-09T11:30:46Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
