How can i access error individual attributes in golang

huangapple go评论136阅读模式
英文:

How can i access error individual attributes in golang

问题

我是你的中文翻译助手,以下是翻译好的内容:

!! 我是Go语言的新手 !!

我正在使用datasync API来启动一个任务执行,这个过程没有问题。但是我在处理返回的错误结构时遇到了困难,我想访问单独的元素,但似乎无法做到。

例如,在下面的错误中,我想要访问**Message_**的内容:

  1. 2022/03/19 09:33:48 Sync called :
  2. 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.
  3. {
  4. RespMetadata: {
  5. StatusCode: 400,
  6. RequestID: "xxxxxxxxxxxxxxxxxxxxx"
  7. },
  8. ErrorCode: "DedupeFailed",
  9. 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."
  10. }

这是我的工作示例:

  1. // 创建datasync服务客户端
  2. svc := datasync.New(sess)
  3. params := &datasync.StartTaskExecutionInput{
  4. TaskArn : aws.String("arn:aws:datasync:*******************************"),
  5. }
  6. // 启动任务执行
  7. resp, err := svc.StartTaskExecution(params)
  8. //err = req.Send()
  9. if err == nil { // resp现在已经填充
  10. fmt.Println(resp) // 这将输出 { TaskExecutionArn: "arn:aws:datasync:xxxxxxxx:task/task-03ecb7728e984e36a/execution/exec-xxxxxxxxxx" }
  11. } else {
  12. fmt.Println(err)
  13. //fmt.Println(err.Message()) 这个不起作用
  14. //fmt.Println(err.Message_) 这个也不起作用
  15. }

如果我执行fmt.Println(err.Message())或者fmt.Println(err.Message_),我会得到以下错误信息:error err.Message undefined (type error has no field or method Message) err.Message_ undefined (type error has no field or method Message_)

我做错了什么?

英文:

!! 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_

  1. 2022/03/19 09:33:48 Sync called :
  2. 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.
  3. {
  4. RespMetadata: {
  5. StatusCode: 400,
  6. RequestID: "xxxxxxxxxxxxxxxxxxxxx"
  7. },
  8. ErrorCode: "DedupeFailed",
  9. 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."
  10. }

Here is my working exemple :

  1. // Create datasync service client
  2. svc := datasync.New(sess)
  3. params := &datasync.StartTaskExecutionInput{
  4. TaskArn : aws.String("arn:aws:datasync:*******************************"),
  5. }
  6. // start task execution
  7. resp, err := svc.StartTaskExecution(params)
  8. //err = req.Send()
  9. if err == nil { // resp is now filled
  10. fmt.Println(resp) // this outputs this { TaskExecutionArn: "arn:aws:datasync:xxxxxxxx:task/task-03ecb7728e984e36a/execution/exec-xxxxxxxxxx" }
  11. } else {
  12. fmt.Println(err)
  13. //fmt.Println(err.Message()) THIS DOES NOT WORK
  14. //fmt.Println(err.Message_) THIS ALSO DOES NOT WORK
  15. }

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 ?

答案1

得分: 4

AWS SDK for Go中的错误通常是awserr.Error接口(Github上的代码)。

如果你只想获取错误消息,可以这样做:

  1. resp, err := svc.StartTaskExecution(params)
  2. if err != nil {
  3. if awsErr, ok := err.(awserr.Error); ok {
  4. fmt.Println(awsErr.Message())
  5. } else {
  6. fmt.Println(err.Error())
  7. }
  8. }

首先,检查是否有错误发生:

  1. if err != nil {...}

然后,我们尝试将错误转换为特定的"type" awserr.Error

  1. err.(awserr.Error)

转换的返回值是特定的错误awsErr和一个bool来指示转换是否成功(ok)。

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

代码的其余部分基本上只是检查ok == true,如果是这种情况,你可以访问错误的字段,比如Message

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

否则,只需打印标准的Go错误消息:

  1. if awsErr, ok := err.(awserr.Error); ok {
  2. ...
  3. } else {
  4. fmt.Println(err.Error())
  5. }
英文:

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:

  1. resp, err := svc.StartTaskExecution(params)
  2. if err != nil {
  3. if awsErr, ok := err.(awserr.Error); ok {
  4. fmt.Println(awsErr.Message())
  5. } else {
  6. fmt.Println(err.Error())
  7. }
  8. }

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

  1. if err != nil {...}

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

  1. 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).

  1. 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:

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

Otherwise, you just print the standard Go error message:

  1. if awsErr, ok := err.(awserr.Error); ok {
  2. ...
  3. } else {
  4. fmt.Println(err.Error())
  5. }

huangapple
  • 本文由 发表于 2022年3月19日 16:49:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/71536694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定