1

This is how error looks like, I just need "Body" when I try fmt.Println(err)

Console

Expected HTTP response code [200] when accessing [POST http://controller:8774/v2.1/os-keypairs], but got 409 instead
{"conflictingRequest": {"message": "Key pair 'Darkhaa test hi' already exists.", "code": 409}}

Controller

createKeyPair, err := compute.CreateKeypair(raw["keyPairName"].(string))

if err != nil {
    fmt.Println(err)
    lists["retType"] = -1
    lists["retDesc"] = err
} else {
    lists["retType"] = 0
    lists["retDesc"] = ""
    lists["retData"] = createKeyPair
}

i.Data["json"] = lists

1 Answer 1

1
type ErrorStruct struct {
    ConflictingRequest struct {
        Message string `json:"message"`
        Code    int    `json:"code"`
    } `json:"conflictingRequest"`
}

go func() {
    _, err := compute.CreateKeypair(raw["keyPairName"].(string))
    if err != nil {
        re := regexp.MustCompile("\\{(.*?)\\}")
        match := re.FindStringSubmatch(err.Error())
        data := ErrorStruct{}
        json.Unmarshal([]byte(match[1]), data)
        log.Printf("Message: %s", data.Message)
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

cannot use err (type error) as type string in argument to re.FindStringSubmatch
data.Message undefined (type ErrorStruct has no field or method Message)
use err.Error()
type error interface { Error() string } you can get string out of error by following error intrface
i edited code re.FindStringSubmatch(err.Error())
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.