saoshyant 0.0.1 → 1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 485baa3c186fc7b8e07b473e9bd065cd8e1920ac1007d3724850f898245289fb
4
- data.tar.gz: 77f6240f7aea6db393e7e45521d1120d90cd623e58a9f89de9f1618a3bf441b8
3
+ metadata.gz: 1e8f5039563d5adf7b102e4735e3af9f7ac53b003ec45116e49b8bfb2556a5b9
4
+ data.tar.gz: a7510586753167922756af504696d79e707b65e43baecba6b9be4120565b6b46
5
5
  SHA512:
6
- metadata.gz: 87d8930e77392ebadc6648975a42bee4076be78f1ed47a75f7435dfb52edf2be91210f3956f16ccc979c45f87a8fd8f584b219df1800fa708531a88c40df61fb
7
- data.tar.gz: 8c23bf5fae6955ce874c4751448a8cd3a727292e413dc320a3724ab1655a10ff979eb7738f0fdb5dfdd87f71ee91a34e8b5a2a9558764f5bea8121c0ed5cd958
6
+ metadata.gz: 8a557ab6a290bd574415a47f05791be506a465cc5f39df37005e26ff3d5c2c9a55fc1d50194b01de286b3bae12847fe22dfd7e9083356812528e8fdbdb1eae40
7
+ data.tar.gz: 0af188cc8d2ca50bc0462781c5b3d8188dc987021c8041baa22c9b360d1ebd5a36e05b634123124fbb13681f08e7f430a95cbfe77da4bb06d14a33f3bf6eca62
data/.bundle/config ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_WITH: "development:test"
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
@@ -0,0 +1,19 @@
1
+ module Saoshyant
2
+ class ExceptionHandler
3
+
4
+ DEFAULT_ERROR_CODE = 500
5
+
6
+ def self.code_status exception, exception_klasses
7
+ exception_klasses.key?(exception.class) ? exception_klasses[exception.class][:code] : DEFAULT_ERROR_CODE
8
+ end
9
+
10
+ def self.log_status exception, exception_klasses
11
+ exception_klasses.key?(exception.class) ? exception_klasses[exception.class][:log] : false
12
+ end
13
+
14
+ def self.validate_arguments code, log
15
+ raise "Invalid code status" unless code.kind_of?(Integer)
16
+ raise "Invalid log status, it should be Boolean" unless !!log == log
17
+ end
18
+ end
19
+ end
data/lib/saoshyant.rb CHANGED
@@ -1,19 +1,34 @@
1
1
  require 'rails'
2
2
  require 'exception_logger'
3
+ require 'exception_handler'
3
4
 
4
5
  module Saoshyant
5
6
  extend ActiveSupport::Concern
7
+ include ActiveSupport::Rescuable
8
+
9
+ @@exception_klasses = {}
10
+ @@block = nil
6
11
 
7
- DEFAULT_ERROR_CODE = 500
8
12
  included do
9
- rescue_from Exception, with: :render_exception
13
+ rescue_from Exception, with: :execute_exception
14
+ end
15
+
16
+ def saoshyant_option(exception_klass, code, log = false)
17
+ Saoshyant::ExceptionHandler.validate_arguments(code, log)
18
+ return if @@exception_klasses.key?(exception_klass)
19
+
20
+ @@exception_klasses.merge!(exception_klass => {code: code, log: log})
21
+ end
22
+
23
+ def saoshyant(&block)
24
+ @@block = block
10
25
  end
11
26
 
12
- def render_exception exception
13
- code = exception.respond_to?(:code) ? exception&.code : DEFAULT_ERROR_CODE
14
- log = exception.respond_to?(:log)
27
+ def execute_exception ex
28
+ code = Saoshyant::ExceptionHandler.code_status ex, @@exception_klasses
29
+ log = Saoshyant::ExceptionHandler.log_status ex, @@exception_klasses
15
30
 
16
- Saoshyant::MajidLogger.log(exception.message) if log == true
17
- render json: {:message => exception.message}, status: code
31
+ Saoshyant::ExceptionLogger.log(ex.message) if log == true
32
+ @@block.call(code, ex.message, ex.class.inspect)
18
33
  end
19
34
  end
data/saoshyant.gemspec CHANGED
@@ -1,10 +1,8 @@
1
- # require 'version'
2
-
3
1
  Gem::Specification.new do |s|
4
2
  s.name = 'saoshyant'
5
- s.version = '0.0.1'
3
+ s.version = '1.2'
6
4
  s.date = '2020-02-10'
7
- s.summary = "Handle Rails Exception Apis"
5
+ s.summary = "Handle Rails Exception"
8
6
  s.description = "when exceptoin raised we dont want see it in ugly and red display, saoshyant present it in json response"
9
7
  s.authors = ["Majid Imanzade"]
10
8
  s.email = 'majidimanzade1@gmail.com'
@@ -13,4 +11,7 @@ Gem::Specification.new do |s|
13
11
 
14
12
  s.files = `git ls-files`.split("\n")
15
13
  s.require_paths = ['lib']
16
- end
14
+ s.add_development_dependency("bundler")
15
+ s.add_development_dependency("rake")
16
+ s.add_development_dependency("rspec")
17
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+ include Saoshyant
3
+
4
+ describe Saoshyant do
5
+ describe 'JsonExceptionHandeler' do
6
+ it 'expect get error for invalid code status' do
7
+ expect{ saoshyant_option(NameError, '200', false) }.to raise_error(RuntimeError)
8
+ end
9
+
10
+ it 'expect get error for invalid log status' do
11
+ expect{ saoshyant_option(NameError, 203, 'true') }.to raise_error(RuntimeError)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'saoshyant'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: saoshyant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: '1.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Majid Imanzade
@@ -9,7 +9,49 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2020-02-10 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
13
55
  description: when exceptoin raised we dont want see it in ugly and red display, saoshyant
14
56
  present it in json response
15
57
  email: majidimanzade1@gmail.com
@@ -17,12 +59,16 @@ executables: []
17
59
  extensions: []
18
60
  extra_rdoc_files: []
19
61
  files:
62
+ - ".bundle/config"
20
63
  - ".gitignore"
64
+ - ".rspec"
21
65
  - Gemfile
66
+ - lib/exception_handler.rb
22
67
  - lib/exception_logger.rb
23
68
  - lib/saoshyant.rb
24
- - lib/version.rb
25
69
  - saoshyant.gemspec
70
+ - spec/saoshyant_test.rb
71
+ - spec/spec_helper.rb
26
72
  homepage: https://rubygems.org/gems/saoshyant
27
73
  licenses:
28
74
  - MIT
@@ -45,5 +91,5 @@ requirements: []
45
91
  rubygems_version: 3.1.2
46
92
  signing_key:
47
93
  specification_version: 4
48
- summary: Handle Rails Exception Apis
94
+ summary: Handle Rails Exception
49
95
  test_files: []
data/lib/version.rb DELETED
@@ -1,5 +0,0 @@
1
- module Saoshyant
2
- class Version
3
- GEM_VERSION = "0.0.1"
4
- end
5
- end