View a markdown version of this page

中的結構化錯誤輸出 AWS CLI - AWS Command Line Interface

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

中的結構化錯誤輸出 AWS CLI

本主題說明 AWS Command Line Interface () 的結構化錯誤輸出格式AWS CLI。CLI 會將錯誤寫入 stderr,並支援下列格式:

  • enhanced (預設) – 顯示內嵌其他詳細資訊的錯誤訊息。用於人類可讀取偵錯。

  • json – 輸出格式為包含所有錯誤欄位的 JSON 字串。使用 進行自動化和指令碼編寫。

  • yaml – 輸出會格式化為包含所有錯誤欄位的 YAML 字串。使用 進行自動化和指令碼編寫。

  • text – 使用文字格式化器格式化錯誤。使用 進行快速視覺化掃描。

  • table – 使用資料表格式化器格式化錯誤。使用 進行快速視覺化掃描。

  • legacy – 沒有結構化詳細資訊的原始錯誤格式。使用 實現回溯相容性。

設定錯誤格式

您可以使用下列任一方法設定錯誤格式:

命令列旗標
$ aws <command> --cli-error-format json
組態檔案 (~/.aws/config)
[default] cli_error_format = json
環境變數
$ export AWS_CLI_ERROR_FORMAT=yaml

錯誤輸出格式

下列各節說明每種格式:

增強型格式 (預設)

增強型格式會顯示錯誤訊息,其中包含簡單值的其他詳細資訊內嵌。對於複雜的結構, 格式會提供使用 JSON 或 YAML 的提示。

範例:缺少區域組態

aws: [ERROR]: An error occurred (NoRegion): You must specify a region. You can also configure your region by running "aws configure".

範例:不存在的 Lambda 函數與其他欄位

aws: [ERROR]: An error occurred (ResourceNotFoundException) when calling the GetFunction operation: Function not found: arn:aws:lambda:us-west-2:123456789012:function:nonexistent-function-12345 Additional error details: Type: User

「其他錯誤詳細資訊」區段只會顯示服務錯誤形狀模型中定義的欄位。不會顯示來自錯誤回應的未建模欄位。

範例:複雜錯誤欄位

An error occurred (TransactionCanceledException) when calling the TransactWriteItems operation: Transaction cancelled, please refer cancellation reasons for specific reasons [ConditionalCheckFailed, None] Additional error details: CancellationReasons: <complex value> Use "--cli-error-format json" or another error format to see the full details.

JSON format (JSON 格式)

JSON 格式提供包含所有錯誤欄位的結構化表示。

範例:缺少區域組態

{ "Code": "NoRegion", "Message": "You must specify a region. You can also configure your region by running \"aws configure\"." }

範例:不存在的 Lambda 函數

{ "Code": "ResourceNotFoundException", "Message": "Function not found: arn:aws:lambda:us-west-2:123456789012:function:nonexistent-function-12345", "Type": "User" }

YAML 格式

YAML 格式提供包含所有錯誤欄位的結構化表示。

範例:缺少區域組態

Code: NoRegion Message: You must specify a region. You can also configure your region by running "aws configure".

範例:不存在的 Lambda 函數

Code: ResourceNotFoundException Message: "Function not found: arn:aws:lambda:us-west-2:123456789012:function:nonexistent-function-12345" Type: User

文字格式

文字格式使用與成功命令輸出相同的格式器。

範例:不存在的 Lambda 函數

ResourceNotFoundException Function not found: arn:aws:lambda:us-west-2:123456789012:function:nonexistent-function-12345 User

資料表格式

資料表格式使用與成功命令輸出相同的格式器。

範例:不存在的 Lambda 函數

------------------------------------------------------------------------------------------------------------------------------------| | error | +----------------------------+--------------------------------------------------------------------------------------------------+------+ | Code | Message | Type | +----------------------------+--------------------------------------------------------------------------------------------------+------+ | ResourceNotFoundException | Function not found: arn:aws:lambda:us-west-2:123456789012:function:nonexistent-function-12345 | User | +----------------------------+--------------------------------------------------------------------------------------------------+------+

舊版格式

舊版格式提供不含結構化詳細資訊的原始錯誤格式。此格式不包含 CLI 例外狀況的「發生錯誤 (ErrorCode):」字首。

範例:缺少區域組態

aws: [ERROR]: You must specify a region. You can also configure your region by running "aws configure".

範例:不存在的 Lambda 函數

An error occurred (ResourceNotFoundException) when calling the GetFunction operation: Function not found: arn:aws:lambda:us-west-2:123456789012:function:nonexistent-function-12345
注意

錯誤現在會持續包含 CLI 例外狀況的aws: [ERROR]:字首。舊版不一定包含此字首。

無論設定的錯誤格式為何,下列例外一律使用舊版格式:

  • UnknownArgumentError – 顯示用量資訊

  • 鍵盤中斷 (KeyboardInterrupt)

完成範例

下列範例顯示具有 JSON 錯誤格式的命令:

$ aws lambda get-function \ --function-name nonexistent-function-12345 \ --cli-error-format json

輸出 (stderr):

{ "Code": "ResourceNotFoundException", "Message": "Function not found: arn:aws:lambda:us-west-2:123456789012:function:nonexistent-function-12345", "Type": "User" }

Type 欄位是 Lambda 服務錯誤形狀中定義的模型化錯誤成員。只有服務錯誤模型中定義的欄位才會包含在結構化錯誤輸出中。