How to fix UnhandledPromiseRejectionWarning: Error: Something went wrong in Node.js

Node.jsINTERMEDIATEHIGHUpdated Dec 9, 2025· 177 views

This warning occurs when a Promise is rejected but no error handler catches the rejection. Starting from Node.js 15, unhandled promise rejections cause the application to terminate, making proper error handling critical for application stability.

Quick fix

Fixes UnhandledPromiseRejectionWarning: Error: Something went wrong

// Before (causes warning)
fetchData()
  .then(data => processData(data))
  .then(result => console.log(result));

// After (handles errors)
fetchData()
  .then(data => processData(data))
  .then(result => console.log(result))
  .catch(error => {
    console.error('Error processing data:', error);
    // Handle error appropriately
  });
  1. Add .catch() handlers to Promise chains
  2. Wrap await calls in try/catch blocks
  3. Add error handlers when calling async functions
  4. Use a global unhandledRejection handler
node.js — error
// Before (causes warning)
fetchData()
.then(data => processData(data))
.then(result => console.log(result));
// After (handles errors)
fetchData()
.then(data => processData(data))
.then(result => console.log(result))
.catch(error => {
console.error('Error processing data:', error);
// Handle error appropriately
});
UnhandledPromiseRejectionWarning: Error: Something went wrong

What this error means

The UnhandledPromiseRejectionWarning is emitted by the Node.js runtime when a Promise is rejected but no rejection handler (like .catch() or try/catch) is attached to handle the error. This indicates a gap in error handling that can lead to silent failures or, in Node.js 15 and later versions, application termination. When JavaScript executes asynchronous code using Promises or async/await, errors must be explicitly caught. Unlike synchronous code where uncaught exceptions bubble up, rejected Promises require explicit error handlers. Without them, the error remains "unhandled" and Node.js emits this warning to alert developers of the potential problem. In earlier versions of Node.js (before v15), unhandled rejections would only log a warning but allow the application to continue. However, this behavior changed in Node.js 15 where unhandled rejections now cause the process to exit with a non-zero status code, making this error critical to address in production environments.

Typical symptoms

  • Warning message appears in console logs: "UnhandledPromiseRejectionWarning"
  • Application exits unexpectedly in Node.js 15+ when the rejection occurs
  • Stack trace points to async code without error handlers
  • Error occurs even though code appears to be working correctly
  • Intermittent crashes in production environments under specific conditions
  • DeprecationWarning about unhandled rejections terminating the process

Common causes

  1. Async function or Promise chain missing .catch() handler
  2. Using await without wrapping it in try/catch block
  3. Error thrown inside a Promise constructor without rejection handler
  4. Promise chain where intermediate promises lack error handling
  5. Error occurring in an error handler itself (nested error)
  6. Callback-style function converted to Promise without proper error handling
  7. Third-party library returning unhandled rejected Promises
  8. Race conditions where errors occur after Promise resolution is expected

How to fix "UnhandledPromiseRejectionWarning: Error: Something went wrong"

1Add .catch() handlers to Promise chains

Ensure every Promise chain has a .catch() handler at the end:

javascript
// Before (causes warning)
fetchData()
  .then(data => processData(data))
  .then(result => console.log(result));

// After (handles errors)
fetchData()
  .then(data => processData(data))
  .then(result => console.log(result))
  .catch(error => {
    console.error('Error processing data:', error);
    // Handle error appropriately
  });

This catches any error that occurs at any point in the Promise chain.

2Wrap await calls in try/catch blocks

For async/await syntax, always use try/catch:

javascript
// Before (causes warning)
async function getData() {
  const data = await fetchData();
  const result = await processData(data);
  return result;
}

// After (handles errors)
async function getData() {
  try {
    const data = await fetchData();
    const result = await processData(data);
    return result;
  } catch (error) {
    console.error('Error in getData:', error);
    throw error; // Re-throw or handle as needed
  }
}
3Add error handlers when calling async functions

When calling async functions, handle their potential rejections:

javascript
// Before (causes warning)
async function main() {
  await getData();
}
main(); // No error handling!

// After (handles errors)
async function main() {
  try {
    await getData();
  } catch (error) {
    console.error('Error in main:', error);
    process.exit(1);
  }
}

// Or with .catch()
main().catch(error => {
  console.error('Error in main:', error);
  process.exit(1);
});
4Use a global unhandledRejection handler

Add a process-level handler as a safety net (not a replacement for proper error handling):

javascript
process.on('unhandledRejection', (reason, promise) => {
  console.error('Unhandled Rejection at:', promise, 'reason:', reason);
  // Log to error monitoring service
  // DO NOT use this as primary error handling
  // Consider exiting the process:
  process.exit(1);
});

This catches any rejections that slip through, but proper error handling in your code is still essential.

5Use ESLint to detect unhandled Promises

Configure ESLint with rules to catch unhandled Promises during development:

bash
npm install --save-dev eslint @typescript-eslint/eslint-plugin

Add to your .eslintrc.json:

json
{
  "rules": {
    "no-floating-promises": "error",
    "@typescript-eslint/no-floating-promises": "error",
    "promise/catch-or-return": "error"
  },
  "plugins": ["promise", "@typescript-eslint"]
}

This prevents unhandled Promises from reaching production.

Advanced notes

Promise Constructor Anti-pattern: Avoid creating Promises manually unless necessary. If you do, ensure both resolve and reject paths are handled:

javascript
// Be careful with Promise constructor
new Promise((resolve, reject) => {
  someAsyncOperation((err, result) => {
    if (err) return reject(err); // Must handle this rejection
    resolve(result);
  });
}).catch(error => console.error(error)); // Don't forget this

Node.js Version Behavior: The handling of unhandled rejections has evolved:
- Node.js <15: Warning only, process continues
- Node.js 15+: Process terminates by default
- Use --unhandled-rejections=warn flag to restore old behavior (not recommended)

Error in Error Handlers: A common subtle issue is errors occurring within catch blocks themselves:

javascript
fetchData()
  .catch(error => {
    // If this JSON.parse fails, it creates another unhandled rejection
    const parsed = JSON.parse(error.message);
  });

// Solution: wrap error handling logic
fetchData()
  .catch(error => {
    try {
      const parsed = JSON.parse(error.message);
    } catch (parseError) {
      console.error('Error parsing error message:', parseError);
    }
  });

Async Event Handlers: Event emitters with async listeners need special care:

javascript
// Problematic
emitter.on('data', async (data) => {
  await processData(data); // Unhandled if this rejects
});

// Better
emitter.on('data', async (data) => {
  try {
    await processData(data);
  } catch (error) {
    console.error('Error processing data:', error);
  }
});

Testing: Use tools like node --trace-warnings to get full stack traces showing where unhandled rejections originate, making them easier to debug.

Frequently asked questions

What does "UnhandledPromiseRejectionWarning: Error: Something went wrong" mean?
catch() or try/catch) is attached to handle the error. js 15 and later versions, application termination. When JavaScript executes asynchronous code using Promises or async/await, errors must be explicitly caught.
How do I fix "UnhandledPromiseRejectionWarning: Error: Something went wrong"?
This warning occurs when a Promise is rejected but no error handler catches the rejection. Starting from Node.js 15, unhandled promise rejections cause the application to terminate, making proper error handling critical for application stability.
What causes "UnhandledPromiseRejectionWarning: Error: Something went wrong"?
Async function or Promise chain missing .catch() handler Using await without wrapping it in try/catch block Error thrown inside a Promise constructor without rejection handler
Is the "UnhandledPromiseRejectionWarning: Error: Something went wrong" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading