thanksforallthefish
11/03/2021, 2:49 PMResult
and AOP
package com.example.demo
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.stereotype.Component
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
@SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}
@RestController
@RequestMapping("/test")
class TestController(val repository: TestRepository) {
@GetMapping
fun test(@RequestParam(defaultValue = "#{{}}") list: List<Int>) =
repository.getList(list)
.getOrElse { list }
}
@Component
class TestRepository {
fun getList(list: List<Int>): Result<List<Int>> = runCatching { throw RuntimeException("Error") }
}
@Aspect
@Component
open class MyOhMy {
@Around("within(com.example.demo..*Repository+)")
fun aspect(joinPoint: ProceedingJoinPoint): Any? =
joinPoint.runCatching {
proceed()
}.getOrThrow()
}
issue is that getOrThrow
in the aspect actually throws, while I would expect to return a Result<Failure
more in threadthanksforallthefish
11/03/2021, 2:54 PMval result = runCatching { throw RuntimeException("test") }
val wrap = runCatching { result }
println(wrap.getOrThrow())
println(result.getOrNull())
but I think something in ProceedingJoinPoint
messes up with my expectationDibya Ranjan
11/08/2021, 8:54 AMReturns the encapsulated value if this instance represents success or throws the encapsulated exception if it is failure.
This function is shorthand for getOrElse { throw it } (see getOrElse).
there is nothing wrong with the PJP
thanksforallthefish
11/08/2021, 9:03 AMjoinPoint.runCatching { proceed() }.getOrThrow()
my expectation was to a <Result<Result<…>>
, where Result<…>
is a Failure
, but I think because of mangling (which fyi is not superclear to me), that gets unwrapped and I get Result<…>
and then ofc getOrThrow
throwsthanksforallthefish
11/08/2021, 9:04 AMval result = runCatching { throw RuntimeException("test") }
val wrap = runCatching { result }
println(wrap.getOrThrow())
println(result.getOrNull())
in this example wrap.getOrThrow
does not throw the exception (as expected)Dibya Ranjan
11/08/2021, 9:05 AMDibya Ranjan
11/08/2021, 9:13 AMval result = runCatching { throw RuntimeException("test") }
val wrap = runCatching { result }
println(wrap.getOrThrow())
println(result.getOrNull())
to
val x = runCatching {
runCatching { throw RuntimeException("Failed") }
}
println(x.getOrThrow())
println(x.getOrNull())
I don’t see .getOrThrow()
called on val result = runCatching { throw RuntimeException("test") }
. 🤔thanksforallthefish
11/08/2021, 9:18 AMval result: Result<…>
and val wrap: Result<Result<…>>
, you have only val x: Result<Result<…>>
the only difference should be that println(result.getOrNull()) //prints null
while println(x.getOrNull()) //prints something else
thanksforallthefish
11/08/2021, 9:20 AMrunCatching { result }
is equivalent to Result.success(result)
, so calling getOrSomething
should return result
(and it does, until that code in used in PJP, then mangling(?) messes things up)