auto-staging/stagectl

View on GitHub
helper/print-api-error.go

Summary

Maintainability
A
0 mins
Test Coverage
package helper

import (
    "encoding/json"
    "log"

    "github.com/auto-staging/stagectl/types"
)

// PrintAPIError unmarshals the Tower API error body and prints the error message to console
func PrintAPIError(body []byte) {
    apiError := types.APIErrorResponse{}
    err := json.Unmarshal(body, &apiError)
    if err != nil {
        log.Fatal(err)
    }
    log.Println("API Error")

    // Output raw json if message was not successfully unmarshaled
    if apiError.Message == "" {
        log.Fatal(string(body))
    }

    log.Fatal(apiError.Message)
}