|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package aws.smithy.kotlin.runtime |
| 6 | + |
| 7 | +import aws.smithy.kotlin.runtime.collections.MutableAttributes |
| 8 | +import kotlin.test.Test |
| 9 | +import kotlin.test.assertEquals |
| 10 | + |
| 11 | +private const val ERROR_CODE = "ErrorWithNoMessage" |
| 12 | +private const val METADATA_MESSAGE = "This is a message included in metadata but not the regular response" |
| 13 | +private const val PROTOCOL_RESPONSE_SUMMARY = "HTTP 418 I'm a teapot" |
| 14 | +private const val REQUEST_ID = "abcd-1234" |
| 15 | +private const val SERVICE_MESSAGE = "This is an service-provided message" |
| 16 | + |
| 17 | +private val ERROR_TYPE = ServiceException.ErrorType.Server |
| 18 | +private val PROTOCOL_RESPONSE = object : ProtocolResponse { |
| 19 | + override val summary: String = PROTOCOL_RESPONSE_SUMMARY |
| 20 | +} |
| 21 | + |
| 22 | +class ExceptionsTest { |
| 23 | + @Test |
| 24 | + fun testRegularMessage() { |
| 25 | + val e = FooServiceException(SERVICE_MESSAGE) |
| 26 | + assertEquals(SERVICE_MESSAGE, e.message) |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + fun testMetadataMessage() { |
| 31 | + val e = FooServiceException { |
| 32 | + set(ServiceErrorMetadata.ErrorMessage, METADATA_MESSAGE) |
| 33 | + } |
| 34 | + assertEquals(METADATA_MESSAGE, e.message) |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + fun testRegularMessageWithRequestId() { |
| 39 | + val e = FooServiceException(SERVICE_MESSAGE) { |
| 40 | + set(ServiceErrorMetadata.RequestId, REQUEST_ID) |
| 41 | + } |
| 42 | + assertEquals("$SERVICE_MESSAGE, Request ID: $REQUEST_ID", e.message) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun testMetadataMessageWithRequestId() { |
| 47 | + val e = FooServiceException { |
| 48 | + set(ServiceErrorMetadata.ErrorMessage, METADATA_MESSAGE) |
| 49 | + set(ServiceErrorMetadata.RequestId, REQUEST_ID) |
| 50 | + } |
| 51 | + assertEquals("$METADATA_MESSAGE, Request ID: $REQUEST_ID", e.message) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun testErrorCodeNoMessage() { |
| 56 | + val e = FooServiceException { |
| 57 | + set(ServiceErrorMetadata.ErrorCode, ERROR_CODE) |
| 58 | + set(ServiceErrorMetadata.ErrorType, ERROR_TYPE) |
| 59 | + set(ServiceErrorMetadata.ProtocolResponse, PROTOCOL_RESPONSE) |
| 60 | + } |
| 61 | + assertEquals( |
| 62 | + "Service returned error code $ERROR_CODE, " + |
| 63 | + "Error type: $ERROR_TYPE, " + |
| 64 | + "Protocol response: $PROTOCOL_RESPONSE_SUMMARY", |
| 65 | + e.message, |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun testErrorCodeNoMessageWithRequestId() { |
| 71 | + val e = FooServiceException { |
| 72 | + set(ServiceErrorMetadata.ErrorCode, ERROR_CODE) |
| 73 | + set(ServiceErrorMetadata.ErrorType, ERROR_TYPE) |
| 74 | + set(ServiceErrorMetadata.ProtocolResponse, PROTOCOL_RESPONSE) |
| 75 | + set(ServiceErrorMetadata.RequestId, REQUEST_ID) |
| 76 | + } |
| 77 | + assertEquals( |
| 78 | + "Service returned error code $ERROR_CODE, " + |
| 79 | + "Error type: $ERROR_TYPE, " + |
| 80 | + "Protocol response: $PROTOCOL_RESPONSE_SUMMARY, " + |
| 81 | + "Request ID: $REQUEST_ID", |
| 82 | + e.message, |
| 83 | + ) |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun testNoErrorCodeNoMessage() { |
| 88 | + val e = FooServiceException { |
| 89 | + set(ServiceErrorMetadata.ErrorType, ERROR_TYPE) |
| 90 | + set(ServiceErrorMetadata.ProtocolResponse, PROTOCOL_RESPONSE) |
| 91 | + } |
| 92 | + assertEquals("Error type: $ERROR_TYPE, Protocol response: $PROTOCOL_RESPONSE_SUMMARY", e.message) |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun testNoErrorCodeNoMessageWithRequestId() { |
| 97 | + val e = FooServiceException { |
| 98 | + set(ServiceErrorMetadata.ErrorType, ERROR_TYPE) |
| 99 | + set(ServiceErrorMetadata.ProtocolResponse, PROTOCOL_RESPONSE) |
| 100 | + set(ServiceErrorMetadata.RequestId, REQUEST_ID) |
| 101 | + } |
| 102 | + assertEquals( |
| 103 | + "Error type: $ERROR_TYPE, Protocol response: $PROTOCOL_RESPONSE_SUMMARY, Request ID: $REQUEST_ID", |
| 104 | + e.message, |
| 105 | + ) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +private class FooServiceException( |
| 110 | + message: String? = null, |
| 111 | + attributes: MutableAttributes.() -> Unit = { }, |
| 112 | +) : ServiceException(message) { |
| 113 | + init { |
| 114 | + sdkErrorMetadata.attributes.apply(attributes) |
| 115 | + } |
| 116 | +} |
0 commit comments