# Display a red pop up on error (routes)

**URL:** <https://community.forestadmin.com/t/display-a-red-pop-up-on-error-routes/2418>\
**Category:** Help me!\
**Tags:** howtos\
**Created:** [May 3, 2021, 8:35pm UTC](https://community.forestadmin.com/t/display-a-red-pop-up-on-error-routes/2418 "2021-05-03T20:35:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emixam23](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/emixam23/32/1350_2.png) [@Emixam23](https://community.forestadmin.com/u/Emixam23)\
**Post date:** [May 3, 2021, 8:35pm UTC](https://community.forestadmin.com/t/display-a-red-pop-up-on-error-routes/2418/1 "2021-05-03T20:35:53Z")

</div>

Hey folks 🙂

I am currently having a behavior that seems a bit weird as I am sure it was working before..

In my route, I, most of the time, need to validate the input data on create/update and so, I am throwing error so the UI displays that red pop up on the bottom right with a text message that indicates an error

Exemple:

```javascript
// Create a Item
router.post('/items', permissionMiddlewareCreator.create(), (request, response, next) => {
    // Learn what this route does here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/routes/default-routes#create-a-record
    throw new Error("Test for error pop-up")
}

```

## Expected behavior

I would expect a pop up when Error are thrown from within a route

## Actual behavior

Only some red output are displayed in the console

## Failure Logs

Sometimes, I even get `Unhandled rejection error` (maybe related to a promise approach I have, i don’t know)

## Context

Package json

```javascript
{
  "name": "my-app",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node ./dist/server.js",
    "legacy-start": "node server.js",
    "build": "tsc",
    "start-dev": "tsc-watch --project ./tsconfig.json --noClear --onSuccess \"nodemon --watch ./dist ./dist/server.js\""
  },
  "dependencies": {
    "@types/express": "^4.17.11",
    "@types/forest-express-mongoose": "^6.3.3",
    "@types/mongoose": "^5.10.3",
    "axios": "^0.21.1",
    "body-parser": "1.19.0",
    "chalk": "~1.1.3",
    "cookie-parser": "1.4.4",
    "cors": "2.8.5",
    "debug": "~4.0.1",
    "dotenv": "~6.1.0",
    "express": "~4.17.1",
    "express-jwt": "5.3.1",
    "forest-express-mongoose": "^6.0.0",
    "mongoose": "~5.8.2",
    "morgan": "1.9.1",
    "nodemon": "^2.0.7",
    "require-all": "^3.0.0",
    "tsc-watch": "^4.2.9",
    "typescript": "^4.1.3"
  }
}

```

Thanks for any help!

Max

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [May 4, 2021, 10:41am UTC](https://community.forestadmin.com/t/display-a-red-pop-up-on-error-routes/2418/2 "2021-05-04T10:41:20Z")

</div>

Hello @Emixam23,

Indeed with don’t have any kind you automatique error handling (middleware) on top of Express. So you can use a [custom middleware](https://expressjs.com/en/guide/error-handling.html) or properly build the response for the client.

> [@Emixam23](#):
>
> `throw new Error("Test for error pop-up")`

Becomes

```javascript
response.status(400).send('Test for error pop-up')

```

I hope this will help you.  
Morgan.

---

<div class="post-metadata">

**Author:** ![Emixam23](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/emixam23/32/1350_2.png) [@Emixam23](https://community.forestadmin.com/u/Emixam23)\
**Post date:** [May 4, 2021, 10:38pm UTC](https://community.forestadmin.com/t/display-a-red-pop-up-on-error-routes/2418/3 "2021-05-04T22:38:05Z")

</div>

Hey there 🙂

Thanks for your feedbacks but… I did find a bug 🙂 Based on your answer, I don’t know how/why, but I saw the light.

I let you test on your end, but I will provided you a model and 2 examples:

**Item.ts**

```javascript
// This model was generated by Lumber. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models
import {Document, model, Schema, Types} from 'mongoose';
import {INote, schema as note} from "./note";

export interface IItem extends Document {
    notes: [INote],
    title: string,
}

// This section contains the properties of your model, mapped to your collection's properties.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const schema = new Schema({
    'notes': [note],
    'title': String
}, {
    timestamps: false,
});

export default model<IItem>('items', schema, 'Items');

```

Of course, this model has a nested notes array, there is the definition:

**Note.ts**

```javascript
import {Document, model, Schema} from 'mongoose';

export interface INote extends Document {
    name: string,
    content: string,
}

// This section contains the properties of your model, mapped to your collection's properties.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
export const schema = new Schema({
    "name": String,
    "content": String,
}, {
    timestamps: false,
    _id: false
});

export default model<INote>('notes', schema)

```

Now that you have the model definition, I let you create one sample 🙂 Let say you create an Item “Test” that has a note with:

- name: Foo
- content: Bar

Now that you have everything setup, let’s create the “inconsistent” behavior (if I may call it like that).

## BUG

Lets get to your `routes/` directory and override the `router.put(...)` (so the update) section of your items:

```javascript
// Update an Item
router.put('/items/:recordId', permissionMiddlewareCreator.update(), (request, response, next) => {
    // Learn what this route does here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/routes/default-routes#update-a-record
    throw new Error("Hello there, let's throw an error!")
});

```

If you now update your previously created record’s title, you will see that red popup, no matter why. You can also use `response.status(400).send("Hello there, let's throw an error!")` and the result will quite be the same, this I agree with you.

However… now lets try to update our “notes” array 🙂

If you update the existing “foo” note we have, it should run smoothly (note that I don’t get why I don’t get the error as it is an update..)

If you create a new note tho… that’s another story 😅 You do get the error displayed in the console, but nothing happens on the UI of ForestAdmin 😕 I should see the popup but I don’t see it

That’s kind of sad because, maybe I am missing something, which is a very possible option, either this part of forest admin is not very well done… I am saying so because, in this same section of the forest admin UI, I had this issue: [Unable to update the layout for sub document definitions (mongodb)](https://community.forestadmin.com/t/unable-to-update-the-layout-for-sub-document-definitions-mongodb/1869) (related to another context, but to the same forest admin UI section/part)

However, thanks for your help/time, we really appreciate! I’ve been testing a couple of BackOffice solution but your is really good, keep moving forward!

Max

---

<div class="post-metadata">

**Author:** ![anon34731316](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@anon34731316](https://community.forestadmin.com/u/anon34731316)\
**Post date:** [May 5, 2021, 4:14pm UTC](https://community.forestadmin.com/t/display-a-red-pop-up-on-error-routes/2418/4 "2021-05-05T16:14:32Z")

</div>

Hi @Emixam23,

Thanks for your detailed message.

You are right, there is a slight inconsistency between the creation and the edition of a sub-document:

- the first one is using the PUT route of the main document (something like: `/forest/items/609274d670cee3ffc9ca1d97`)
- the second one is using the PUT route of the relationship (something like `forest/items/609274d670cee3ffc9ca1d97/relationships/notes/0`).

I am creating a bug report on this so that we can homogenise this behaviour.

Meanwhile, can you tell me if / how this is blocking you achieving your goals? What is your usecase exactly?

Thanks again!
