adislksn / bebii-web-tools-worker

0 stars 0 forks source link

FB API, 502 gateway on production #1

Open adislksn opened 1 day ago

adislksn commented 1 day ago

Error response, cloudflare/docker TODO:

test.damarlampung.my.id | 502: Bad gateway

Bad gateway Error code 502

Visit cloudflare.com for more information.
2024-11-04 23:34:56 UTC
You

Browser

Working
San Jose

Cloudflare

Working
test.damarlampung.my.id

Host

Error

What happened?

The web server reported a bad gateway error.

What can I do?

Please try again in a few minutes.

adislksn commented 1 day ago

Async Error Handler

alternative :

// Global error handling middleware
const errorHandler = (err, req, res, next) => {
  console.error('Error:', err);

  // Handle specific error types
  if (err.name === 'ValidationError') {
    return res.status(400).json({
      status: 'error',
      message: 'Validation Error',
      details: err.message
    });
  }

  if (err.name === 'UnauthorizedError') {
    return res.status(401).json({
      status: 'error',
      message: 'Unauthorized',
      details: err.message
    });
  }

  // Default error response
  res.status(500).json({
    status: 'error',
    message: 'Internal Server Error',
    details: process.env.NODE_ENV === 'development' ? err.message : undefined
  });
};

// Async handler wrapper
const asyncHandler = (fn) => (req, res, next) => {
  Promise.resolve(fn(req, res, next)).catch(next);
};

// Example usage in your routes
app.use('/api/v1', router);
app.use(errorHandler);

// Example route with proper error handling
router.get('/example', asyncHandler(async (req, res) => {
  // Your async code here
  const result = await someAsyncOperation();
  res.json(result);
}));

// Alternative try-catch approach for individual routes
router.get('/another-example', async (req, res, next) => {
  try {
    const result = await someAsyncOperation();
    res.json(result);
  } catch (error) {
    next(error);
  }
});