-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue-1438: Fix upstream service error represented as 400 (#1570)
* issue-1438- new error type for upstream service error * Issue-1438: handle new error type and return 500 * issue-1438-formatted files * issue-1438: undo formatting errors * issue-1438-add missing import * issue-1438: implemented RawErrorResponse error object * issue-1438: add more test cases * issue-1438: update tests and code clean up * issue-1438: code clean up * issue-1438: add missing headers * issue-1438: format files * refactor FailedDecodeAttempt * make the RawErrorResponse case class more binary compatibility-friendly * issue-1438: make all field private * issue-1438: unapply method for RawResponse * issue-1438: make unapply private * issue-1438: fixed tests * issue-1438: fixed tests * issue-1438: make field public * Add changelog entry * Expose fields directly * Redo the docs a bit, add more private constructors --------- Co-authored-by: Jakub Kozłowski <[email protected]>
- Loading branch information
1 parent
6b7ac73
commit bc83380
Showing
11 changed files
with
396 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright 2021-2024 Disney Streaming | ||
* | ||
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://disneystreaming.github.io/TOST-1.0.txt | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package smithy4s.http | ||
|
||
import scala.annotation.nowarn | ||
|
||
final case class RawErrorResponse private ( | ||
code: Int, | ||
headers: Map[CaseInsensitive, Seq[String]], | ||
body: String, | ||
failedDecodeAttempt: FailedDecodeAttempt | ||
) extends Throwable { | ||
|
||
def withCode(code: Int): RawErrorResponse = | ||
copy(code = code) | ||
|
||
def withHeaders( | ||
headers: Map[CaseInsensitive, Seq[String]] | ||
): RawErrorResponse = | ||
copy(headers = headers) | ||
|
||
def withBody(body: String): RawErrorResponse = | ||
copy(body = body) | ||
|
||
def withFailedDecodeAttempt( | ||
failedDecodeAttempt: FailedDecodeAttempt | ||
): RawErrorResponse = | ||
copy(failedDecodeAttempt = failedDecodeAttempt) | ||
|
||
override def getMessage(): String = { | ||
val baseMessage = s"status $code, headers: $headers, body:\n$body" | ||
baseMessage + s""" | ||
|FailedDecodeAttempt: | ||
| ${failedDecodeAttempt.getMessage} | ||
""".stripMargin | ||
} | ||
|
||
override def getCause: Throwable = failedDecodeAttempt | ||
} | ||
|
||
object RawErrorResponse { | ||
def apply( | ||
code: Int, | ||
headers: Map[CaseInsensitive, Seq[String]], | ||
body: String, | ||
failedDecodeAttempt: FailedDecodeAttempt | ||
): RawErrorResponse = | ||
new RawErrorResponse(code, headers, body, failedDecodeAttempt) | ||
|
||
@nowarn | ||
private def unapply(response: RawErrorResponse): Option[ | ||
(Int, Map[CaseInsensitive, Seq[String]], String, FailedDecodeAttempt) | ||
] = | ||
Some( | ||
( | ||
response.code, | ||
response.headers, | ||
response.body, | ||
response.failedDecodeAttempt | ||
) | ||
) | ||
|
||
} | ||
|
||
sealed trait FailedDecodeAttempt extends Throwable { | ||
def discriminator: HttpDiscriminator | ||
def getMessage: String | ||
} | ||
|
||
object FailedDecodeAttempt { | ||
|
||
final case class UnrecognisedDiscriminator private ( | ||
discriminator: HttpDiscriminator | ||
) extends FailedDecodeAttempt { | ||
|
||
def withDiscriminator( | ||
discriminator: HttpDiscriminator | ||
): UnrecognisedDiscriminator = | ||
copy(discriminator = discriminator) | ||
|
||
override def getMessage: String = | ||
s"Unrecognised discriminator: $discriminator" | ||
} | ||
|
||
object UnrecognisedDiscriminator { | ||
def apply(discriminator: HttpDiscriminator): UnrecognisedDiscriminator = | ||
new UnrecognisedDiscriminator(discriminator) | ||
} | ||
|
||
final case class DecodingFailure private ( | ||
discriminator: HttpDiscriminator, | ||
contractError: HttpContractError | ||
) extends FailedDecodeAttempt { | ||
|
||
def withDiscriminator(discriminator: HttpDiscriminator): DecodingFailure = | ||
copy(discriminator = discriminator) | ||
|
||
def withContractError(contractError: HttpContractError): DecodingFailure = | ||
copy(contractError = contractError) | ||
|
||
override def getMessage: String = | ||
s"Decoding failed for discriminator: $discriminator with error: ${contractError.getMessage}" | ||
} | ||
|
||
object DecodingFailure { | ||
def apply( | ||
discriminator: HttpDiscriminator, | ||
contractError: HttpContractError | ||
): DecodingFailure = | ||
new DecodingFailure(discriminator, contractError) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.