Get error message during build
Asked by Attila Berczik:

I am trying to send a Google Chat message with the status of the build once it finishes, particularly I am interested in the error message.

I am building in Bitbucket Pipelines, and I have found the afterscript for that.

```
              script:

                  - echo "run!"

              after-script:

                  - chmod +x build/after.sh

                  - build/after.sh

```

With the $BITBUCKET_EXIT_CODE environment I can determine whether the build was successful, and I want to access the logs. Based on [this answer](https://community.atlassian.com/t5/Bitbucket-questions/Access-to-Bitbucket-Pipelines-error-information-on-failure-of-a/qaq-p/1767292) I was able to build this code.

```
PIPELINE_UUID=$(echo "${BITBUCKET_PIPELINE_UUID}"| cut -c 2-37)

STEP_UUID=$(echo "${BITBUCKET_STEP_UUID}"| cut -c 2-37)

LOG_API=$(echo "https://api.bitbucket.org/2.0/repositories/${BITBUCKET_REPO_FULL_NAME}/pipelines/$PIPELINE_UUID/steps/$STEP_UUID/log")

curl --request GET --url "$LOG_API"
```

This creates an url like this, but I constantly receive an error.  
[https://api.bitbucket.org/2.0/repositories/b..../u...../pipelines/83a864de-4635-408c-99ef-47247c6b5eb1/steps/a24ab199-c965-4a40-b855-34a1bfd985d5/log](https://api.bitbucket.org/2.0/repositories/beskgroup/unified-accounting/pipelines/83a864de-4635-408c-99ef-47247c6b5eb1/steps/a24ab199-c965-4a40-b855-34a1bfd985d5/log)  

    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                     Dload  Upload   Total   Spent    Left  Speed

      0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

    100    61  100    61    0     0   1033      0 --:--:-- --:--:-- --:--:--  1033

    {"type": "error", "error": {"message": "Resource not found"}}

![Screenshot 2022-05-10 201251.png](https://community.atlassian.com/t5/image/serverpage/image-id/196712iD61E267D69691C38/image-size/large?v=v2&px=999 "Screenshot 2022-05-10 201251.png")

I am pretty sure that these are the correct details to access the logs, I would appreciate any help. ??

<br />

Otherwise I have thought about saving error messages as a file during the build, then save it as an artifact, so the after-script has access to it, but I found no way of doing this, a suggestion from this side could also help.


Answers:
author: Patrik S
body: Hello [@Attila Berczik](/t5/user/viewprofilepage/user-id/4564431) ,

Thank you for reaching out to Atlassian Community!

The reason why the API call to get the logs is failing is because the step is only completed after the **Build teardown**, which runs at the very end on each step. Before that, the logs will not be available in our backend and thus cannot be fetched from the API.

My suggestion in this case would be to output the logs of the commands you are executing in the step to a file by using the **tee**command :

    command |& tee -a output.txt

The `|& tee -a` command will make both the standard output and standard error streams to be copied to the file while still being visible in the UI. If the file already exists, the new data will get appended to the end of the file.

The output.txt file can then be accessed in the after-script section, so you can POST it to an external endpoint. Please find an example YML file below :

```
image: atlassian/default-image:3

pipelines:
   default:
      - step:
          script:
            - echo "test log" |& tee -a output.txt
            - echo "test log 2" |& tee -a output.txt
          after-script:
           - cat output.txt
```

Hope that helps! Let me know in case you have any questions related to this topic.

Thank you [@Attila Berczik](/t5/user/viewprofilepage/user-id/4564431) .

Kind regards,

Patrik S

comments: author: Attila Berczik
body: Thank you very much for your help, it helped tremendously.

Do you have an idea how could I use this command without distorting the exit code?

```
- echo "test log 2" |& tee -a output.txt
```

With this the errors get lost and the Pipeline runs successfully all the time, I somehow have to return a 1 exit code.

I have thought about checking the file if it contains the error word, but it doesn't work in all cases.  

```
if [[ $OUTPUT == *"ERROR"* ]]; then exit 1; fi
```

Could you help me with this too?

Thank you!


Tags: api

bitbucket-cloud

bitbucket-pipelines

build

cloud

continuous-delivery

error

log

script