mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 10:28:27 +00:00
Return 400 when input is invalid instead of server error
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
package docspell.restserver.routes
|
package docspell.restserver.routes
|
||||||
|
|
||||||
import cats.data.NonEmptyList
|
import cats.data.{EitherT, NonEmptyList}
|
||||||
import cats.effect._
|
import cats.effect._
|
||||||
import cats.implicits._
|
import cats.implicits._
|
||||||
|
|
||||||
@ -64,17 +64,29 @@ object NotificationRoutes extends NonEmptyListSupport {
|
|||||||
case req @ POST -> Root =>
|
case req @ POST -> Root =>
|
||||||
for {
|
for {
|
||||||
input <- req.as[NotificationChannel]
|
input <- req.as[NotificationChannel]
|
||||||
ch <- Sync[F].pure(NotificationChannel.convert(input)).rethrow
|
ch <- Sync[F].pure(NotificationChannel.convert(input))
|
||||||
res <- backend.notification.createChannel(ch, user.account)
|
resp <- EitherT
|
||||||
resp <- Ok(Conversions.basicResult(res, "Channel created"))
|
.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
|
} yield resp
|
||||||
|
|
||||||
case req @ PUT -> Root =>
|
case req @ PUT -> Root =>
|
||||||
for {
|
for {
|
||||||
input <- req.as[NotificationChannel]
|
input <- req.as[NotificationChannel]
|
||||||
ch <- Sync[F].pure(NotificationChannel.convert(input)).rethrow
|
ch <- Sync[F].pure(NotificationChannel.convert(input))
|
||||||
res <- backend.notification.updateChannel(ch, user.account)
|
resp <- EitherT
|
||||||
resp <- Ok(Conversions.basicResult(res, "Channel created"))
|
.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
|
} yield resp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user