Skip to content

Commit dad0fbd

Browse files
committed
Rename Dsl.TryCatch to keywords.TryCatch.DslComposer
1 parent 8cb39e8 commit dad0fbd

File tree

4 files changed

+238
-171
lines changed

4 files changed

+238
-171
lines changed

Dsl/src/main/scala/com/thoughtworks/dsl/Dsl.scala

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -408,130 +408,6 @@ object Dsl extends LowPriorityDsl0 {
408408
}
409409
}
410410

411-
@implicitNotFound(
412-
"The `try` ... `catch` expression cannot contain !-notation inside a function that returns ${OuterDomain}."
413-
)
414-
@deprecated(
415-
"Dsl.TryCatch / Dsl.TryFinally / Dsl.TryCatchFinally will be removed",
416-
"2.0.0"
417-
)
418-
trait TryCatch[Value, OuterDomain, BlockDomain] {
419-
def tryCatch(
420-
block: BlockDomain !! Value,
421-
catcher: Catcher[BlockDomain !! Value],
422-
outerSuccessHandler: Value => OuterDomain
423-
): OuterDomain
424-
}
425-
426-
private[dsl] trait LowPriorityTryCatch {
427-
implicit def liftFunction1TryCatch[Value, OuterDomain, BlockDomain, State](
428-
implicit restTryCatch: TryCatch[Value, OuterDomain, BlockDomain]
429-
): TryCatch[Value, State => OuterDomain, State => BlockDomain] = {
430-
(
431-
block: (State => BlockDomain) !! Value,
432-
catcher: Catcher[(State => BlockDomain) !! Value],
433-
outerSuccessHandler: Value => State => OuterDomain
434-
) => (state: State) =>
435-
def withState(blockContinuation: (State => BlockDomain) !! Value) = {
436-
(blockHandler: (Value => BlockDomain)) =>
437-
blockContinuation { (value: Value) => (state: State) =>
438-
blockHandler(value)
439-
}(state)
440-
}
441-
442-
restTryCatch.tryCatch(
443-
withState(block),
444-
catcher.andThen(withState _),
445-
outerSuccessHandler(_)(state)
446-
)
447-
}
448-
}
449-
450-
object TryCatch extends LowPriorityTryCatch {
451-
452-
implicit def throwableContinuationTryCatch[LeftDomain, Value]
453-
: TryCatch[Value, LeftDomain !! Throwable, LeftDomain !! Throwable] = {
454-
455-
(
456-
block: LeftDomain !! Throwable !! Value,
457-
catcher: Catcher[LeftDomain !! Throwable !! Value],
458-
outerSuccessHandler: Value => LeftDomain !! Throwable
459-
) => outerFailureHandler =>
460-
def innerFailureHandler(e: Throwable): LeftDomain = {
461-
catcher.lift(e) match {
462-
case None =>
463-
outerFailureHandler(e)
464-
case Some(recovered) =>
465-
@inline
466-
def recoveredHandler(): LeftDomain = {
467-
locally {
468-
try {
469-
recovered(outerSuccessHandler)
470-
} catch {
471-
case NonFatal(nativeThrown) =>
472-
return outerFailureHandler(nativeThrown)
473-
}
474-
}(outerFailureHandler)
475-
}
476-
477-
recoveredHandler()
478-
}
479-
}
480-
481-
def runBlock(): LeftDomain = {
482-
(try {
483-
block { a => hookedFailureHandler =>
484-
@inline
485-
def successHandler(): LeftDomain = {
486-
locally {
487-
try {
488-
outerSuccessHandler(a)
489-
} catch {
490-
case NonFatal(nativeThrown) =>
491-
return outerFailureHandler(nativeThrown)
492-
}
493-
}(outerFailureHandler)
494-
}
495-
496-
successHandler()
497-
}
498-
} catch {
499-
case NonFatal(e) =>
500-
return innerFailureHandler(e)
501-
})(innerFailureHandler)
502-
}
503-
runBlock()
504-
}
505-
506-
implicit def futureTryCatch[BlockValue, OuterValue](implicit
507-
executionContext: ExecutionContext
508-
): TryCatch[BlockValue, Future[OuterValue], Future[BlockValue]] = {
509-
(
510-
block: Future[BlockValue] !! BlockValue,
511-
catcher: Catcher[Future[BlockValue] !! BlockValue],
512-
outerSuccessHandler: BlockValue => Future[OuterValue]
513-
) =>
514-
catchNativeException(block)
515-
.recoverWith { case e: Throwable =>
516-
def recover(): Future[BlockValue] = {
517-
(try {
518-
catcher.lift(e)
519-
} catch {
520-
case NonFatal(extractorException) =>
521-
return Future.failed(extractorException)
522-
}) match {
523-
case None =>
524-
Future.failed(e)
525-
case Some(recovered) =>
526-
catchNativeException(recovered)
527-
}
528-
}
529-
recover()
530-
}
531-
.flatMap(outerSuccessHandler)
532-
}
533-
}
534-
535411
@implicitNotFound(
536412
"The `try` ... `finally` expression cannot contain !-notation inside a function that returns ${OuterDomain}."
537413
)

build.sbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ lazy val `domains-scalaz` =
186186
reset % Test,
187187
`keywords-Monadic`,
188188
`keywords-Return`,
189+
`keywords-TryCatch`,
190+
`keywords-TryFinally`,
189191
`keywords-Shift` % Test,
190192
`keywords-Yield` % Test
191193
)

domains-scalaz/src/main/scala/com/thoughtworks/dsl/domains/scalaz.scala

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package com.thoughtworks.dsl.domains
1+
package com.thoughtworks.dsl
2+
package domains
23

34
import com.thoughtworks.dsl.Dsl
45
import com.thoughtworks.dsl.Dsl.!!
@@ -145,7 +146,6 @@ object scalaz extends scalaz.LowPriority0 {
145146
monadThrowable.raiseError(e)
146147
}
147148
}
148-
149149
implicit def scalazTryFinally[F[_], A, B](implicit
150150
monadError: MonadThrowable[F]
151151
): TryFinally[A, F[B], F[A], F[Unit]] =
@@ -172,36 +172,39 @@ object scalaz extends scalaz.LowPriority0 {
172172
}
173173
}
174174

175-
implicit def scalazTryCatch[F[_], A, B](implicit
175+
given [F[_], A, B](using
176176
monadError: MonadThrowable[F]
177-
): TryCatch[A, F[B], F[A]] =
178-
new TryCatch[A, F[B], F[A]] {
179-
def tryCatch(
180-
block: F[A] !! A,
181-
catcher: Catcher[F[A] !! A],
182-
outerSuccessHandler: A => F[B]
183-
): F[B] = {
184-
import monadError.monadErrorSyntax._
185-
catchNativeException(block)
186-
.handleError { e =>
187-
def recover(): F[A] = {
188-
(try {
189-
catcher.lift(e)
190-
} catch {
191-
case NonFatal(extractorException) =>
192-
return monadError.raiseError(extractorException)
193-
}) match {
194-
case None =>
195-
monadError.raiseError(e)
196-
case Some(recovered) =>
197-
catchNativeException(recovered)
198-
}
199-
}
200-
recover()
201-
}
202-
.flatMap(outerSuccessHandler)
203-
}
177+
): keywords.TryCatch.DslComposer[F[B], A, F[A]] =
178+
keywords.TryCatch.DslComposer {
179+
[BlockKeyword, CaseKeyword] =>
180+
(
181+
blockDsl: Dsl.Searching[BlockKeyword, F[A], A],
182+
caseDsl: Dsl.Searching[CaseKeyword, F[A], A]
183+
) ?=>
184+
Dsl.Composed[keywords.TryCatch[BlockKeyword, CaseKeyword], F[B], A] {
185+
case (keywords.TryCatch(block, catcher), outerSuccessHandler) =>
186+
import monadError.monadErrorSyntax._
187+
summon[Dsl.Run[BlockKeyword, F[A], A]](block)
188+
.handleError { e =>
189+
def recover(): F[A] = {
190+
(try {
191+
catcher.lift(e)
192+
} catch {
193+
case NonFatal(extractorException) =>
194+
return monadError.raiseError(extractorException)
195+
}) match {
196+
case None =>
197+
monadError.raiseError(e)
198+
case Some(recovered) =>
199+
summon[Dsl.Run[CaseKeyword, F[A], A]](recovered)
200+
}
201+
}
202+
recover()
203+
}
204+
.flatMap(outerSuccessHandler)
205+
}
204206
}
207+
205208
private[scalaz] trait LowPriority0:
206209
/** The [[Dsl]] instance that converts a keyword to the monad domain type
207210
* then flatMap. This instance helps when the keyword supports a domain `D`

0 commit comments

Comments
 (0)