aws-cloudformation / aws-cloudformation/cloudformation-cli-python-plugin

Better example code?

未關閉
#53 1 則留言 0 個 reaction 已指派 1 人 已被 @ericzbeard 認領 在 GitHub 檢視
documentation good first issue p1
主要語言
Python
星號
107
分支
46
PR 合併指標
30 天內沒有已合併 PR

描述

This could also be misunderstanding, the current example code in the create_handler looks like:

```python
try:
if isinstance(session, SessionProxy):
client = session.client("s3")
# Setting Status to success will signal to cfn that the operation is complete
progress.status = OperationStatus.SUCCESS
except TypeError as e:
# exceptions module lets CloudFormation know the type of failure that occurred
raise exceptions.InternalFailure(f"was not expecting type {e}")
# this can also be done by returning a failed progress event
# return ProgressEvent.failed(HandlerErrorCode.InternalFailure, f"was not expecting type {e}")
return progress
```
as far as I can see that will never raise a TypeError. So I feel this code will be better to get people started:

```python
if not isinstance(session, SessionProxy):
# exceptions module lets CloudFormation know the type of failure that occurred
raise exceptions.InternalFailure(f"session should be a SessionProxy")
# this can also be done by returning a failed progress event
# return ProgressEvent.failed(HandlerErrorCode.InternalFailure, f"was not expecting type {e}")

# TODO: put code here

# Setting Status to success will signal to cfn that the operation is complete
progress.status = OperationStatus.SUCCESS
return progress
```

That shows the same concepts, without having that try except increasing indentation.

Even if it would throw that exception, I don't see the added value of replacing that with a custom exception (it makes debugging harder if we do not re-raise the original exception)

If CloudFormation requires the thrown exception to be from the exceptions, this sort of code will lead to pokemon exception handling (gotta catch them all), and converting unknown exceptions to InternlFailure would be better handled in `cloudformation-cli-python-lib`

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。