# Custom exceptions in Nest

**URL:** <https://discuss.elastic.co/t/custom-exceptions-in-nest/99115>\
**Category:** Elasticsearch\
**Created:** [September 1, 2017, 10:43am UTC](https://discuss.elastic.co/t/custom-exceptions-in-nest/99115 "2017-09-01T10:43:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjgasior](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@mjgasior](https://discuss.elastic.co/u/mjgasior)\
**Post date:** [September 1, 2017, 10:43am UTC](https://discuss.elastic.co/t/custom-exceptions-in-nest/99115/1 "2017-09-01T10:43:37Z")

</div>

In the documentation it's possible to define custom exceptions via **ConnectionConfiguration** as said [here](https://www.elastic.co/guide/en/elasticsearch/client/net-api/1.x/errors.html):

```
var settings = new ConnectionConfiguration()
    .SetConnectionStatusHandler(r=> {
        if (r.HttpStatusCode == 403)
           throw new MyApplicationNotLoggedInException();
        });

```

But **ConnectionConfiguration** is the way of setting up the low level API. Currently I use only Nest and my only option is to redefine **ConnectionSettings** where my only option is **.ThrowExceptions(bool)**. Is there any option to use the low level api settings in Nest?

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [September 2, 2017, 8:43am UTC](https://discuss.elastic.co/t/custom-exceptions-in-nest/99115/2 "2017-09-02T08:43:28Z")

</div>

`ConnectionSettings` in NEST implements the same interface as `ConnectionConfiguration`, so the same methods available on it are also available on `ConnectionSettings`.

The documentation you're looking at is from 1.x. To [throw a custom exception](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/elasticsearch-net-getting-started.html#_handling_errors) in NEST 2.x onwards would be

```
var connectionSettings = new ConnectionSettings()
    .OnRequestCompleted(response =>
    {
        if (response.HttpStatusCode == 403)
            throw new MyApplicationNotLoggedInException();
    });

var client = new ElasticClient(connectionSettings);

try 
{
    var searchResponse = client.Search<Message>();
}
catch (MyApplicationNotLoggedInException e)
{
    // 403 error
}
```

---

<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:** [September 30, 2017, 8:43am UTC](https://discuss.elastic.co/t/custom-exceptions-in-nest/99115/3 "2017-09-30T08:43:29Z")

</div>

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