中的結構化錯誤輸出 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".

範例:不存在的 S3 儲存貯體與其他欄位

aws: [ERROR]: An error occurred (NoSuchBucket) when calling the GetObject operation: The specified bucket does not exist Additional error details: BucketName: amzn-s3-demo-bucket

範例:複雜錯誤欄位

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\"." }

範例:不存在的 S3 儲存貯體

{ "Code": "NoSuchBucket", "Message": "The specified bucket does not exist", "BucketName": "amzn-s3-demo-bucket" }

YAML 格式

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

範例:缺少區域組態

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

範例:不存在的 S3 儲存貯體

Code: NoSuchBucket Message: The specified bucket does not exist BucketName: amzn-s3-demo-bucket

文字格式

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

範例:不存在的 S3 儲存貯體

amzn-s3-demo-bucket NoSuchBucket The specified bucket does not exist

資料表格式

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

範例:不存在的 S3 儲存貯體

-------------------------------------------------------------------------------------| | error | +---------------------------+---------------+----------------------------------------+ | BucketName | Code | Message | +---------------------------+---------------+----------------------------------------+ | amzn-s3-demo-bucket | NoSuchBucket | The specified bucket does not exist | +---------------------------+---------------+----------------------------------------+

舊版格式

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

範例:缺少區域組態

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

範例:不存在的 S3 儲存貯體

An error occurred (NoSuchBucket) when calling the GetObject operation: The specified bucket does not exist
注意

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

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

  • UnknownArgumentError – 顯示用量資訊

  • 鍵盤中斷 (KeyboardInterrupt)

完成範例

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

$ aws s3api get-object \ --bucket amzn-s3-demo-bucket \ --key file.txt out.txt \ --cli-error-format json

輸出 (stderr):

{ "Code": "NoSuchBucket", "Message": "The specified bucket does not exist", "BucketName": "amzn-s3-demo-bucket" }

BucketName 欄位是由 Amazon S3 服務傳回的模型化錯誤成員。