> For the complete documentation index, see [llms.txt](https://bothosting-net.gitbook.io/bot-hosting.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bothosting-net.gitbook.io/bot-hosting.net/troubleshooting/identify-errors.md).

# Identify Errors

Add useful error logging and identify the root cause from your server logs.

## Before you start

* Understand the code you are hosting.
* Use a text editor or IDE (recommended) to update your files.

### Add error logging

Server logs show why an application stops or behaves unexpectedly. Add logging to your main entry file before your application starts.

{% hint style="warning" %}
Logging an uncaught exception does not make the application safe to continue. Fix the root cause, then restart the server.
{% endhint %}

#### Node.js exceptions and promise rejections

```javascript
// Logs synchronous runtime errors that reach the event loop.
process.on("uncaughtException", (error, origin) => {
  console.error(`[CRITICAL ERROR] Uncaught Exception at: ${origin}`);
  console.error(error.stack || error);
});

// Logs promise rejections that have no error handler.
process.on("unhandledRejection", (reason, promise) => {
  console.error("[CRITICAL ERROR] Unhandled Promise Rejection:", promise);
  console.error("Reason:", reason?.stack || reason);
});
```

#### Discord.js REST event logging

Use REST events to log specific response codes and take action based on them.

```javascript
client.rest.on(RESTEvents.Response, (Req, Resp) => {
    if (!(Resp.status === 429)) return;
    console.log('[429] Rate limited!')
});
```

### Identify the cause

1. Restart the server and reproduce the problem.
2. Review the first error in the console output.
3. Trace the stack back to your code or configuration.

The final error can be a symptom. Earlier log entries often show the root cause.

Enhanced version that also created a log file: <https://codump.github.io/code/a-fix-to-log-missed-errors-and-create-a-log-file-for-all-console.log-error/>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://bothosting-net.gitbook.io/bot-hosting.net/troubleshooting/identify-errors.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
