# Getting job failure message to send via Slack Orb (4.3.1)

**URL:** <https://discuss.circleci.com/t/getting-job-failure-message-to-send-via-slack-orb-4-3-1/39633>\
**Category:** Orbs\
**Created:** [April 2, 2021, 5:03pm UTC](https://discuss.circleci.com/t/getting-job-failure-message-to-send-via-slack-orb-4-3-1/39633 "2021-04-02T17:03:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jakeatoms](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/jakeatoms/32/24064_2.png) [@jakeatoms](https://discuss.circleci.com/u/jakeatoms)\
**Post date:** [April 2, 2021, 5:03pm UTC](https://discuss.circleci.com/t/getting-job-failure-message-to-send-via-slack-orb-4-3-1/39633/1 "2021-04-02T17:03:00Z")

</div>

We’re starting to use the Slack orb and are currently using the supplied templates. However, we’d like to see the failure reason in the Slack message for convenience.

I would imagine that getting the failure reason when it’s due to compilation failure would require scripting on my part and then storing the message in an environment variable to access in the `slack/notify` step. However, when it’s a CircleCI failure like `Too long with no output: Context deadline exceeded`, is there a way to access that within the config?

Has anyone else successfully sent failure reasons via the Slack orb?

---

<div class="post-metadata">

**Author:** ![aaronclark](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/aaronclark/32/31078_2.png) [@aaronclark](https://discuss.circleci.com/u/aaronclark)\
**Post date:** [April 6, 2021, 3:14am UTC](https://discuss.circleci.com/t/getting-job-failure-message-to-send-via-slack-orb-4-3-1/39633/2 "2021-04-06T03:14:45Z")

</div>

Hi!

I believe this would be possible by utilizing our API.

The information for a job can be obtained in the following way:

First you will need to get the build information for the step that failed.  
You could get that information by running a query like this:

`curl https://circleci.com/api/v1.1/project/github/{orgname}/{project name}/ -H "Circle-Token: <circle-token>"`

Then you can filter the results for steps where `failed` is true, and get the url in `build_url`.

Following that, you can run the following query using the url from above:

`curl https://circleci.com/api/v1.1/project/github/{orgname}/{project name}/{job number} -H "Circle-Token: <circle-token>"`

This will give you a json response, which contains a URL to the output log of each step under the key `output_url`.

You could then curl that log, and parse out the information you need for your error message.  
After that, you could pass in the message into a variable. Since CircleCI uses a different shell environment for each step, you will need a step like this in your config:

`echo "export SLACK_ERROR_MESSAGE='${SLACK_MESSAGE}'" >> $BASH_ENV`  
where `SLACK_MESSAGE` is where you stored the output from the log, and `SLACK_ERROR_MESSAGE` is the variable you will use to insert the message into your slack orb template.

It is probably possible to make this more dynamic (such as utilizing the `CIRCLE_BUILD_URL` and other environment variables made available in all builds) so that you don’t have to hardcode in the org/project names.

Let us know if this helps point you in the right direction.

---

<div class="post-metadata">

**Author:** ![aaronclark](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/aaronclark/32/31078_2.png) [@aaronclark](https://discuss.circleci.com/u/aaronclark)\
**Post date:** [April 6, 2021, 5:44am UTC](https://discuss.circleci.com/t/getting-job-failure-message-to-send-via-slack-orb-4-3-1/39633/3 "2021-04-06T05:44:15Z")

</div>

I took a look at some of our other posts, and noticed that we have a Support Solution post regarding this topic. I think it explains in more detail the process I was describing, so please took a look:

> [@Leveraging CircleCI API to include build logs in Slack notifications](https://discuss.circleci.com/t/leveraging-circleci-api-to-include-build-logs-in-slack-notifications/39111):
>
> Leveraging CircleCI API to include build logs in Slack notifications The [Slack Orb](https://circleci.com/developer/orbs/orb/circleci/slack) allows you to send notifications, and customize the message sent. In most cases, this is used to indicate a build status, such as if it failed. While the above is helpful, it still requires you to navigate back to the build to get information about why it failed. Wouldn’t it be nice if you could include the last few lines of the log output with your Slack message? This could help easily identify the issue, such …

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/circleci/original/3X/5/1/511608acda8e85906e15909b57bd0a46c1dbe291.png) [@system](https://discuss.circleci.com/u/system)\
**Post date:** [April 16, 2021, 5:44am UTC](https://discuss.circleci.com/t/getting-job-failure-message-to-send-via-slack-orb-4-3-1/39633/4 "2021-04-16T05:44:19Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
