Skip to content

Commit

Permalink
Merge pull request #63 from Sija/develop
Browse files Browse the repository at this point in the history
v1.6
  • Loading branch information
Sija committed Feb 13, 2020
2 parents 5850245 + 8047e7a commit 16d04fa
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 13 deletions.
6 changes: 3 additions & 3 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: raven
version: 1.5.5
version: 1.6.0

authors:
- Sijawusz Pur Rahnama <sija@sija.pl>
Expand All @@ -12,7 +12,7 @@ dependencies:
development_dependencies:
timecop:
github: crystal-community/timecop.cr
version: ~> 0.3.0
version: ~> 0.4.0
ameba:
github: crystal-ameba/ameba
version: ~> 0.11.0
Expand All @@ -21,6 +21,6 @@ targets:
crash_handler:
main: src/crash_handler.cr

crystal: 0.30.0
crystal: 0.32.0

license: MIT
38 changes: 38 additions & 0 deletions spec/raven/context_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require "../spec_helper"

describe Raven::Context do
{% for key in %i(user extra tags) %}
{% env_key = "SENTRY_CONTEXT_#{key.upcase.id}" %}

context %(with ENV["{{ env_key.id }}"]?) do
it "initializes from valid JSON-encoded string" do
with_clean_env do
ENV[{{ env_key }}] = {foo: :bar}.to_json

context = Raven::Context.new
context.{{ key.id }}.should eq({"foo" => "bar"})
end
end

it "raises when JSON-encoded string is not a Hash" do
with_clean_env do
ENV[{{ env_key }}] = (0..3).to_a.to_json

expect_raises(Raven::Error, {{ env_key }}) do
Raven::Context.new
end
end
end

it "raises on invalid JSON-encoded string" do
with_clean_env do
ENV[{{ env_key }}] = "foo"

expect_raises(Raven::Error, {{ env_key }}) do
Raven::Context.new
end
end
end
end
{% end %}
end
18 changes: 18 additions & 0 deletions spec/raven/instance_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ describe Raven::Instance do
end
end
end

it "use passed values only within the block" do
with_instance do |instance|
Raven::Context.clear!
Raven.{{key.id}}_context(will: :stay_there)
ctx = Raven.{{key.id}}_context(foo: :foo, bar: :bar) do
instance.capture("Test message", {{key.id}}: {bar: "baz"}) do |event|
event.{{key.id}}.should eq({
:will => :stay_there,
:foo => :foo,
:bar => "baz"
})
end
end
ctx.should eq({:will => :stay_there})
Raven.{{key.id}}_context.should be(ctx)
end
end
end
{% end %}

Expand Down
10 changes: 3 additions & 7 deletions src/crash_handler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ module Raven
property logger : ::Logger {
Logger.new({{ "STDOUT".id unless flag?(:release) }}).tap do |logger|
logger.level = {{ flag?(:debug) ? "Logger::DEBUG".id : "Logger::ERROR".id }}

"#{logger.progname}.crash_handler".tap do |progname|
logger.progname = progname
configuration.exclude_loggers << progname
end
logger.progname = "raven.crash_handler"
end
}

Expand Down Expand Up @@ -141,8 +137,8 @@ module Raven
private def run_process(error : IO = IO::Memory.new)
@process_status = Process.run command: name, args: args,
shell: true,
input: STDIN,
output: STDOUT,
input: Process::Redirect::Inherit,
output: Process::Redirect::Inherit,
error: IO::MultiWriter.new(STDERR, error)
error.to_s.chomp
end
Expand Down
23 changes: 21 additions & 2 deletions src/raven/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,29 @@ module Raven
any_json_property :contexts, :extra, :tags, :user

def initialize
@contexts = {
self.contexts = {
os: self.class.os_context,
runtime: self.class.runtime_context,
}.to_any_json
}
initialize_from_env
end

protected def initialize_from_env
{% for key in %i(user extra tags) %}
{% env_key = "SENTRY_CONTEXT_#{key.upcase.id}" %}

if %context = ENV[{{ env_key }}]?.presence
begin
if %json = JSON.parse(%context).as_h?
self.{{ key.id }}.merge!(%json)
else
raise Raven::Error.new("`{{ env_key.id }}` must contain a JSON-encoded hash")
end
rescue %e : JSON::ParseException
raise Raven::Error.new("Invalid JSON string in `{{ env_key.id }}`", cause: %e)
end
end
{% end %}
end
end
end
17 changes: 17 additions & 0 deletions src/raven/instance.cr
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,23 @@ module Raven
context.extra.merge!(hash, options)
end

{% for key in %i(user extra tags) %}
# Bind {{ key.id }} context.
# Merges with existing context (if any).
#
# See `#{{ key.id }}_context`
def {{ key.id }}_context(hash = nil, **options)
prev_context = context.{{ key.id }}.clone
begin
context.{{ key.id }}.merge!(hash, options)
yield
ensure
context.{{ key.id }} = prev_context
end
context.{{ key.id }}
end
{% end %}

def breadcrumbs
BreadcrumbBuffer.current
end
Expand Down
2 changes: 1 addition & 1 deletion src/raven/version.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Raven
VERSION = "1.5.5"
VERSION = "1.6.0"
end

0 comments on commit 16d04fa

Please sign in to comment.