Skip to content

Commit

Permalink
Error handling utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
essenciary committed Mar 18, 2024
1 parent 7310709 commit aa35716
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
@@ -1,7 +1,7 @@
name = "Genie"
uuid = "c43c736e-a2d1-11e8-161f-af95117fbd1e"
authors = ["Adrian Salceanu <adrian@genieframework.com> and the amazing Genie contributors ♥️"]
version = "5.27.0"
version = "5.28.0"

[deps]
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
Expand Down
59 changes: 59 additions & 0 deletions src/Exceptions.jl
Expand Up @@ -4,6 +4,7 @@ import Genie
import HTTP

export ExceptionalResponse, RuntimeException, InternalServerException, NotFoundException, FileExistsException
export @log!!throw, @try!


"""
Expand Down Expand Up @@ -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

2 comments on commit aa35716

@essenciary
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/103116

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v5.28.0 -m "<description of version>" aa3571643a0c2a898062f9ab9e6a81e87a583220
git push origin v5.28.0

Please sign in to comment.