amazon-web-servicesgoerror-handlingaws-datasync

How can i access error individual attributes in golang


!! i Am new to go !!

I Am using the datasync API to start a task execution , that works with no issues. I Am struggling with the returned error struct, i want to acces individual elements but i can't seem to be a ble to do so.

for exemple in the following error i want to be able to access the content of Message_

2022/03/19 09:33:48 Sync called : 
InvalidRequestException: Unable to queue the task execution for task task-xxxxxxxxxxxx. The task already has another task execution exec-030b4a31dc2e33641 currently running or queued with identical Include and Exclude filter patterns. Please provide unique Include and Exclude filter patterns for the new task execution and try again.
{
  RespMetadata: {
    StatusCode: 400,
    RequestID: "xxxxxxxxxxxxxxxxxxxxx"
  },
  ErrorCode: "DedupeFailed",
  Message_: "Unable to queue the task execution for task task-xxxxxxxxxx. The task already has another task execution exec-xxxxxxxxxx currently running or queued with identical Include and Exclude filter patterns. Please provide unique Include and Exclude filter patterns for the new task execution and try again."
}

Here is my working exemple :

    // Create datasync service client
    svc := datasync.New(sess)

    params := &datasync.StartTaskExecutionInput{
        TaskArn : aws.String("arn:aws:datasync:*******************************"),
    }

    // start task execution
    resp, err := svc.StartTaskExecution(params)

    //err = req.Send()

    if err == nil { // resp is now filled
        fmt.Println(resp)  // this outputs this { TaskExecutionArn: "arn:aws:datasync:xxxxxxxx:task/task-03ecb7728e984e36a/execution/exec-xxxxxxxxxx" }

    } else {
        fmt.Println(err)
        //fmt.Println(err.Message()) THIS DOES NOT WORK
        //fmt.Println(err.Message_)  THIS ALSO DOES NOT WORK
    }

If I do this fmt.Println(err.Message()) or this fmt.Println(err.Message_) I get this error err.Message undefined (type error has no field or method Message) err.Message_ undefined (type error has no field or method Message_)

Where did I go wrong ?


Solution

  • Errors in the AWS SDK for Go are often of the interface awserr.Error (Code on Github).

    If you just want to have the message, you can do this:

    resp, err := svc.StartTaskExecution(params)
    
    if err != nil {
        if awsErr, ok := err.(awserr.Error); ok {
            fmt.Println(awsErr.Message())
        } else {
            fmt.Println(err.Error())
        }
    }
    

    First, there is a check if there is actually an error:

    if err != nil {...}
    

    Then, we try to cast the error to it's specific "type" awserr.Error:

    err.(awserr.Error)
    

    The return value of cast is the specific error awsErr and a bool to indicate if the cast worked or not (ok).

    awsErr, ok := err.(awserr.Error)
    

    The rest of the code is basically just checking, if ok == true and if that is the case you can access the errors fields like Message:

    if awsErr, ok := err.(awserr.Error); ok {
        fmt.Println(awsErr.Message())
    }
    

    Otherwise, you just print the standard Go error message:

    if awsErr, ok := err.(awserr.Error); ok {
        ...
    } else {
        fmt.Println(err.Error())
    }