|
| 1 | +import TecoCodeGeneratorTestHelpers |
| 2 | +import XCTest |
| 3 | + |
| 4 | +#if Xcode // Works around FB11980900 |
| 5 | +@testable import teco_common_error_generator |
| 6 | +#else |
| 7 | +@testable import TecoCommonErrorGenerator |
| 8 | +#endif |
| 9 | + |
| 10 | +final class TecoCommonErrorGeneratorTests: XCTestCase { |
| 11 | + func testCommonErrorStructDeclBuilder() { |
| 12 | + let errors = [ |
| 13 | + CommonError(code: "ActionOffline", description: "This API has been deprecated.\n接口已下线。", solution: nil), |
| 14 | + CommonError( |
| 15 | + code: "AuthFailure.SecretIdNotFound", |
| 16 | + description: """ |
| 17 | + Key does not exist. Check if the key has been deleted or disabled in the console, and if not, check if the key is correctly entered. Note that whitespaces should not exist before or after the key. |
| 18 | + 密钥不存在。请在[控制台](https://console.cloud.tencent.com/cam/capi)检查密钥是否已被删除或者禁用,如状态正常,请检查密钥是否填写正确,注意前后不得有空格。 |
| 19 | + """, |
| 20 | + solution: """ |
| 21 | + - The SecretId is not found, please ensure that your SecretId is correct. |
| 22 | + SecretId不存在,请输入正确的密钥。 |
| 23 | + |
| 24 | + 当您接口返回这些错误时,说明您调接口时用的密钥信息不存在,请在控制台检查密钥是否已被删除或者禁用,如状态正常,请检查密钥是否填写正确,注意前后不得有空格。 |
| 25 | + """ |
| 26 | + ), |
| 27 | + ] |
| 28 | + AssertBuilder(buildCommonErrorStructDecl(from: errors), """ |
| 29 | + /// Common error type returned by Tencent Cloud. |
| 30 | + public struct TCCommonError: TCServiceErrorType { |
| 31 | + enum Code: String { |
| 32 | + case actionOffline = "ActionOffline" |
| 33 | + case authFailure_SecretIdNotFound = "AuthFailure.SecretIdNotFound" |
| 34 | + } |
| 35 | + |
| 36 | + private let error: Code |
| 37 | + |
| 38 | + public let context: TCErrorContext? |
| 39 | + |
| 40 | + public var errorCode: String { |
| 41 | + self.error.rawValue |
| 42 | + } |
| 43 | + |
| 44 | + public init?(errorCode: String, context: TCErrorContext) { |
| 45 | + guard let error = Code(rawValue: errorCode) else { |
| 46 | + return nil |
| 47 | + } |
| 48 | + self.error = error |
| 49 | + self.context = context |
| 50 | + } |
| 51 | + |
| 52 | + public func asCommonError() -> TCCommonError? { |
| 53 | + return self |
| 54 | + } |
| 55 | + |
| 56 | + internal init(_ error: Code, context: TCErrorContext? = nil) { |
| 57 | + self.error = error |
| 58 | + self.context = context |
| 59 | + } |
| 60 | + |
| 61 | + /// This API has been deprecated. |
| 62 | + /// 接口已下线。 |
| 63 | + public static var actionOffline: TCCommonError { |
| 64 | + TCCommonError(.actionOffline) |
| 65 | + } |
| 66 | + |
| 67 | + /// Key does not exist. Check if the key has been deleted or disabled in the console, and if not, check if the key is correctly entered. Note that whitespaces should not exist before or after the key. |
| 68 | + /// 密钥不存在。请在[控制台](https://console.cloud.tencent.com/cam/capi)检查密钥是否已被删除或者禁用,如状态正常,请检查密钥是否填写正确,注意前后不得有空格。 |
| 69 | + /// |
| 70 | + /// - The SecretId is not found, please ensure that your SecretId is correct. |
| 71 | + /// SecretId不存在,请输入正确的密钥。 |
| 72 | + /// |
| 73 | + /// 当您接口返回这些错误时,说明您调接口时用的密钥信息不存在,请在控制台检查密钥是否已被删除或者禁用,如状态正常,请检查密钥是否填写正确,注意前后不得有空格。 |
| 74 | + public static var authFailure_SecretIdNotFound: TCCommonError { |
| 75 | + TCCommonError(.authFailure_SecretIdNotFound) |
| 76 | + } |
| 77 | + } |
| 78 | + """) |
| 79 | + } |
| 80 | + |
| 81 | + func testFormatErrorSolution() { |
| 82 | + let solution = """ |
| 83 | + The provided credentials could not be validated. Please check your signature is correct. |
| 84 | + 请求签名验证失败,请检查您的签名计算是否正确。 |
| 85 | + The provided credentials could not be validated because of exceeding request size limit, please use new signature method `TC3-HMAC-SHA256`. |
| 86 | + 由于请求包大小超过限制,请求签名验证失败,请使用新的签名方法 `TC3-HMAC-SHA256`。 |
| 87 | + |
| 88 | + 当您看到此类错误信息,说明此次请求签名计算错误,强烈建议使用官网提供的 SDK 调用,自己计算签名比较容易出错,SDK 屏蔽了计算签名的细节,调用者只需关注接口参数。 |
| 89 | + 如果仍然想自己计算签名,参照官网签名文档,可以在API Explorer【签名串生成】处进行签名验证。 |
| 90 | + 此外,SecretKey输入错误也可能会导致签名计算错误。 |
| 91 | + """ |
| 92 | + XCTAssertEqual(formatErrorSolution(solution), """ |
| 93 | + - The provided credentials could not be validated. Please check your signature is correct. |
| 94 | + 请求签名验证失败,请检查您的签名计算是否正确。 |
| 95 | + - The provided credentials could not be validated because of exceeding request size limit, please use new signature method `TC3-HMAC-SHA256`. |
| 96 | + 由于请求包大小超过限制,请求签名验证失败,请使用新的签名方法 `TC3-HMAC-SHA256`。 |
| 97 | + |
| 98 | + 当您看到此类错误信息,说明此次请求签名计算错误,强烈建议使用官网提供的 SDK 调用,自己计算签名比较容易出错,SDK 屏蔽了计算签名的细节,调用者只需关注接口参数。 |
| 99 | + 如果仍然想自己计算签名,参照官网签名文档,可以在API Explorer【签名串生成】处进行签名验证。 |
| 100 | + 此外,SecretKey输入错误也可能会导致签名计算错误。 |
| 101 | + """) |
| 102 | + } |
| 103 | +} |
0 commit comments