# Reusing common errorOutVariants

**URL:** <https://softwaremill.community/t/reusing-common-erroroutvariants/187>\
**Category:** tapir\
**Created:** [April 13, 2023, 4:44pm UTC](https://softwaremill.community/t/reusing-common-erroroutvariants/187 "2023-04-13T16:44:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![frozenwizard](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@frozenwizard](https://softwaremill.community/u/frozenwizard)\
**Post date:** [April 13, 2023, 4:44pm UTC](https://softwaremill.community/t/reusing-common-erroroutvariants/187/1 "2023-04-13T16:44:25Z")

</div>

Is there a way I can define errorOutVariants in a single place that can be reused on multiple endpoints?

So I have for all my endpoints some kind of authorization error variant, bad requests, server errors that are in all of the endpoints. I also have a specific error out variant for each endpoints.

So I would like to be able to do something like this:

```plaintext

val commonVariants = List(oneOfVariant(BadRequestVariant),
    oneOfVariant(AuthorizationErrorVariant),
    oneOfDefaultVariant(DefaultErrorVariant))

securedEndpoint.get
.blahblahblah
.errorOutVariants(
   oneOfVariant(statusCode(StatusCode.NotFound).and(jsonBody[NotFound]))
)
.errorOutVariants(commonVariants)

```

Potentially even better is when I define my PartialServerEndpoint for authentication, since all my endpoints use that, be able to define that there.

I had tried making it so that securedEndpoint.errorOutVariants() had all my common error outs, but I got runtime errors about them not being defined when I called the endpoint.

---

<div class="post-metadata">

**Author:** ![kciesielski](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/kciesielski/32/133_2.png) [@kciesielski](https://softwaremill.community/u/kciesielski)\
**Post date:** [April 17, 2023, 11:53am UTC](https://softwaremill.community/t/reusing-common-erroroutvariants/187/2 "2023-04-17T11:53:32Z")

</div>

I don’t think you can define variants in a List in a way that you can reuse them later. However, putting them directly in a base `securedEndpoint` should be good enough, you do it once anyway, right?  
Something like:

```auto
val securedEndpoint = 
endpoint
  // here go your base auth & header inputs
      .errorOut(
        oneOf(
          oneOfVariant(statusCode(StatusCode.BadRequest).and(jsonBody[BadRequestError])),
          oneOfVariant(
            statusCode(StatusCode.Unauthorized).and(emptyOutputAs(UnauthorizedErr))
          ),
          oneOfDefaultVariant(statusCode(StatusCode.InternalServerError).and(jsonBody[DefaultError]))
        )
      )

```

Now you can define a specialized endpoint:

```auto
securedEndpoint
.get
.blahblah
.errorOutVariant(oneOfVariant(statusCode(StatusCode.NotFound).and(jsonBody[NotFound])))

```

The documentation mentions reusing error variants here: [One-of variants — tapir 1.x documentation](https://tapir.softwaremill.com/en/latest/endpoint/oneof.html#error-outputs)

---

<div class="post-metadata">

**Author:** ![frozenwizard](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@frozenwizard](https://softwaremill.community/u/frozenwizard)\
**Post date:** [April 17, 2023, 9:02pm UTC](https://softwaremill.community/t/reusing-common-erroroutvariants/187/3 "2023-04-17T21:02:13Z")

</div>

Hey that works. I had been using the errorOutVariant in the securedendpoint and the server kept throwing errors. Thanks!
