diff --git a/Project.toml b/Project.toml index ec1752df6..d5725aaf9 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Genie" uuid = "c43c736e-a2d1-11e8-161f-af95117fbd1e" authors = ["Adrian Salceanu and the amazing Genie contributors ♥️"] -version = "5.27.0" +version = "5.28.0" [deps] ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63" diff --git a/src/Exceptions.jl b/src/Exceptions.jl index 8461f20f2..a906ddb06 100644 --- a/src/Exceptions.jl +++ b/src/Exceptions.jl @@ -4,6 +4,7 @@ import Genie import HTTP export ExceptionalResponse, RuntimeException, InternalServerException, NotFoundException, FileExistsException +export @log!!throw, @try! """ @@ -170,4 +171,62 @@ Custom printing for `FileExistsException` """ Base.show(io::IO, ex::FileExistsException) = print(io, "FileExistsException: $(ex.path)") + +""" + @log!!throw(ex) + +Macro to log an exception if the app is in production, and rethrow it if the app is in dev mode. +""" +macro log!!throw(ex) + quote + if Genie.Configuration.isprod() + @error $ex + else + rethrow($ex) + end + end +end + + +""" + @tried(ex1, ex2) + +Macro to wrap a try/catch block and @log!!throw the exception. + +# Example +```julia +julia> @tried(1+1, (ex) -> @warn(ex)) +2 + +julia> @tried(error("wut?"), (ex) -> @warn(ex)) # in dev mode, exception is rethrown +ERROR: wut? +Stacktrace: + [1] error(s::String) + @ Base ./error.jl:35 + [2] macro expansion + @ REPL[5]:4 [inlined] + [3] top-level scope + @ REPL[14]:1 + +julia> @tried(error("wut?"), (ex) -> @warn(ex)) # in prod mode, exception is logged +┌ Warning: 2024-03-15 13:04:43 ErrorException("wut?") +└ @ Main REPL[16]:1 +``` +""" +macro try!(ex1, ex2) + quote + try + $ex1 + catch e + if Genie.Configuration.isprod() + @error e + else + rethrow(e) + end + + $ex2(e) + end + end |> esc +end + end