This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #250 from alycklama/adds-support-for-returning-the…
…-same-error-across-multiple-status-codes Adds support for returning the same error across multiple status codes
- Loading branch information
Showing
4 changed files
with
98 additions
and
1 deletion.
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
52 changes: 52 additions & 0 deletions
52
core/src/test/resources/api/errors/single_error_for_multiple_status_codes.scala
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,52 @@ | ||
package io.github.ghostbuster91.sttp.client3.example | ||
|
||
import _root_.sttp.client3._ | ||
import _root_.sttp.model._ | ||
import _root_.io.circe.{Error => CirceError} | ||
import _root_.io.circe.Decoder | ||
import _root_.io.circe.Encoder | ||
import _root_.sttp.client3.circe.SttpCirceApi | ||
|
||
trait CirceCodecs extends SttpCirceApi { | ||
implicit lazy val errorModelDecoder: Decoder[ErrorModel] = | ||
Decoder.forProduct1("msg")(ErrorModel.apply) | ||
implicit lazy val errorModelEncoder: Encoder[ErrorModel] = | ||
Encoder.forProduct1("msg")(p => p.msg) | ||
implicit lazy val updatePersonGenericErrorDecoder | ||
: Decoder[UpdatePersonGenericError] = | ||
List[Decoder[UpdatePersonGenericError]]( | ||
Decoder[ErrorModel].asInstanceOf[Decoder[UpdatePersonGenericError]] | ||
).reduceLeft(_ or _) | ||
implicit lazy val updatePersonGenericErrorEncoder | ||
: Encoder[UpdatePersonGenericError] = Encoder.instance { | ||
case errorModel: ErrorModel => | ||
Encoder[ErrorModel].apply(errorModel) | ||
} | ||
} | ||
object CirceCodecs extends CirceCodecs | ||
|
||
sealed trait UpdatePersonGenericError | ||
case class ErrorModel(msg: String) extends UpdatePersonGenericError() | ||
|
||
class DefaultApi(baseUrl: String, circeCodecs: CirceCodecs = CirceCodecs) { | ||
import circeCodecs._ | ||
|
||
def updatePerson(): Request[ | ||
Either[ResponseException[UpdatePersonGenericError, CirceError], Unit], | ||
Any | ||
] = basicRequest | ||
.put(uri"$baseUrl/person") | ||
.response( | ||
fromMetadata( | ||
asJsonEither[UpdatePersonGenericError, Unit], | ||
ConditionalResponseAs( | ||
_.code == StatusCode.unsafeApply(400), | ||
asJsonEither[ErrorModel, Unit] | ||
), | ||
ConditionalResponseAs( | ||
_.code == StatusCode.unsafeApply(401), | ||
asJsonEither[ErrorModel, Unit] | ||
) | ||
) | ||
) | ||
} |
32 changes: 32 additions & 0 deletions
32
core/src/test/resources/api/errors/single_error_for_multiple_status_codes.yaml
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,32 @@ | ||
openapi: 3.0.2 | ||
info: | ||
title: Single Error | ||
version: "1.0" | ||
paths: | ||
/person: | ||
put: | ||
summary: Update an existing person | ||
description: Update an existing person by Id | ||
operationId: updatePerson | ||
responses: | ||
"400": | ||
description: Returns a 400 | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/ErrorModel" | ||
"401": | ||
description: Returns a 401 | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/ErrorModel" | ||
components: | ||
schemas: | ||
ErrorModel: | ||
required: | ||
- msg | ||
type: object | ||
properties: | ||
msg: | ||
type: string |
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