Slack Bolt Error Handling: A Practical Guide
In this article
Slack Bolt error handling means catching failures in your listener middleware, logging them with enough context to debug, and returning a safe response to Slack so users see a clear message instead of a silent failure. The core pattern is to wrap your listener logic in a try/catch, use Bolt's built-in error handler for uncaught exceptions, and acknowledge the request before doing slow work. You will need a Slack app with a bot token and the Bolt library installed for your language (JavaScript, Python, or Java).
What You'll Need
- A Slack app created at api.slack.com/apps with a bot token and the scopes your commands need (for example
chat:writeandcommands). - Slack Bolt installed for your runtime:
@slack/boltfor Node.js,slack-boltfor Python, or the Java SDK. - Your signing secret and bot token stored as environment variables, not hard-coded.
- A local server or tunnel (such as ngrok) for testing request URLs during development.
Step-by-Step
- Install and initialize Bolt with an error handler. In Node.js, pass an
errorHandlerfunction to theAppconstructor. In Python, register a global error handler with@app.error. This catches anything that escapes a listener. Keep the handler short: log the error and its context, then return. - Acknowledge first, then work. For slash commands and interactive components, call
ack()as early as possible. Slack expects a response within 3 seconds. If your handler does database or API work first, Slack will show a timeout error and may retry, causing duplicate actions. Acknowledge, then userespond()orclient.chat.postMessageto send the real result. - Wrap each listener body in try/catch. Catch errors where you have the most context: the command name, the user ID, the channel, and the input. Log those fields with the error. Without them, a stack trace alone rarely tells you which user or command failed.
- Return user-safe messages. Do not send raw exception text to Slack. Map known failures to friendly text (for example, "I could not find that channel") and use a generic fallback for unknown errors. Include a short reference ID if you log one, so support can match the message to the log entry.
- Handle Slack API errors specifically. Bolt surfaces Slack Web API failures as errors with a
codeproperty such aschannel_not_found,not_in_channel, orratelimited. Check forratelimitedand respect theRetry-Afterheader instead of retrying immediately. Fornot_in_channel, invite the bot to the channel rather than retrying blindly. - Validate input before calling the API. Check that required arguments exist, that IDs look plausible, and that the user has permission for the action. Most errors are preventable: a missing subcommand, an empty text field, or a user ID from the wrong workspace. Reject early with a clear message.
- Set up logging with levels. Log expected validation failures at info or warn, and unexpected exceptions at error. Include the Slack team ID, user ID, command, and a timestamp. Avoid logging tokens or full request bodies that may contain sensitive data.
- Test failure paths deliberately. Trigger a bad channel ID, a missing scope, and a forced exception in a development workspace. Confirm the user sees a friendly message, the error handler fires, and the log contains enough to reproduce the problem.
Common Mistakes
- Acknowledging too late. Doing slow work before
ack()causes timeouts and duplicate retries. Acknowledge first, always. - Swallowing errors silently. An empty catch block hides bugs and makes support impossible. At minimum, log the error with context.
- Sending stack traces to users. Raw errors confuse users and can leak internal details. Map to friendly messages.
- Retrying rate-limited calls immediately. Slack returns
ratelimitedwith a retry delay. Ignoring it makes the problem worse. - Forgetting the global error handler. Without it, uncaught exceptions in listeners can crash the process or produce no response at all.
Tips
- Create a small helper that formats error responses consistently, so every listener behaves the same way.
- Use Bolt's built-in middleware for logging and authorization rather than repeating checks in every listener.
- Keep the error handler idempotent: it may run more than once if Slack retries a request.
- Log a correlation ID and include it in the user-facing message when the error is unexpected.
- Test with the same scopes and channel setup as production; missing scopes are a common source of confusing failures.
Troubleshooting
If users see "operation timed out" or the command does nothing, check that ack() is called before any slow work. If the app returns a 500 error, inspect the global error handler and confirm it is registered before listeners run. If Slack reports invalid_auth or not_authed, verify the bot token environment variable is loaded and matches the workspace. If you see missing_scope, add the scope in the app configuration and reinstall the app to the workspace. If errors appear only in production, compare environment variables, installed scopes, and channel membership between environments. For rate limits, check the Retry-After value and add a delay rather than retrying in a loop. If the process crashes on an unhandled rejection, ensure every async listener has a try/catch or that the global handler is wired correctly.
Frequently Asked Questions
What is the difference between ack() and respond() in Bolt?
ack() confirms receipt to Slack within the time limit. respond() sends a follow-up message to the user or channel after acknowledgement. Use both: acknowledge first, respond with results.
Does Bolt retry failed requests automatically?
Slack may retry requests when it does not receive a timely acknowledgement. Bolt itself does not automatically retry your listener logic, so design handlers to be safe if they run more than once.
How do I handle rate limits in Slack Bolt?
Catch the error, check for the ratelimited code, read the retry delay from the response headers, and wait before retrying. Do not loop immediately.
Should I log every error?
Log unexpected errors at error level and expected validation failures at info or warn. Always include context such as user, command, and channel, but never log tokens.
Can I use one error handler for all listeners?
Yes. A global error handler catches uncaught exceptions, while local try/catch blocks handle expected failures with specific messages. Use both together.
What should users see when something fails?
A short, friendly message that explains what happened and what to try next, without internal details. Include a reference ID if you log one.