Skip to content

Commit

Permalink
Return 400 when input is invalid instead of server error
Browse files Browse the repository at this point in the history
  • Loading branch information
eikek committed Jun 4, 2022
1 parent d58bf80 commit 6063cce
Showing 1 changed file with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

package docspell.restserver.routes

import cats.data.NonEmptyList
import cats.data.{EitherT, NonEmptyList}
import cats.effect._
import cats.implicits._

Expand Down Expand Up @@ -64,17 +64,29 @@ object NotificationRoutes extends NonEmptyListSupport {
case req @ POST -> Root =>
for {
input <- req.as[NotificationChannel]
ch <- Sync[F].pure(NotificationChannel.convert(input)).rethrow
res <- backend.notification.createChannel(ch, user.account)
resp <- Ok(Conversions.basicResult(res, "Channel created"))
ch <- Sync[F].pure(NotificationChannel.convert(input))
resp <- EitherT
.fromEither[F](ch)
.semiflatMap { c =>
backend.notification
.createChannel(c, user.account)
.map(res => Conversions.basicResult(res, "Channel created"))
}
.foldF(ex => BadRequest(BasicResult(false, ex.getMessage)), Ok(_))
} yield resp

case req @ PUT -> Root =>
for {
input <- req.as[NotificationChannel]
ch <- Sync[F].pure(NotificationChannel.convert(input)).rethrow
res <- backend.notification.updateChannel(ch, user.account)
resp <- Ok(Conversions.basicResult(res, "Channel created"))
ch <- Sync[F].pure(NotificationChannel.convert(input))
resp <- EitherT
.fromEither[F](ch)
.semiflatMap { c =>
backend.notification
.updateChannel(c, user.account)
.map(res => Conversions.basicResult(res, "Channel created"))
}
.foldF(ex => BadRequest(BasicResult(false, ex.getMessage)), Ok(_))
} yield resp
}
}
Expand Down

0 comments on commit 6063cce

Please sign in to comment.