saoshyant 1.0.1 → 1.2.1

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: a61410a83892b4511dc858709e1033c89da482294e1f1d6f82f8a2b1c207ace4
4
- data.tar.gz: 5a737800386ce2c20444b6ed92d89ed3e75fb4f8e47b73f69c1e8dd8e2e44f54
3
+ metadata.gz: 2148ce17d37d35084ad0b7a83ef4b556d2bf5de51e18cd3e8690f9edddaaf064
4
+ data.tar.gz: 3e53a5664c30efe33da7aa56c1583a3797374e6efcf6275cd06e674d5af68c00
5
5
  SHA512:
6
- metadata.gz: f24478979654ab6feea4b97d6c97749405f532b52298cc13ba701f4a7d69792184dc00467d2f11004d93a12b7011fcb7431553a53740e774d9460199cfe1911e
7
- data.tar.gz: 655f889ffbc6a39ac65a56a32a13dd25221624b438459b6eb0621a34770fa8f3658f91365e6dbd73b6440c2f9b4764bd2fe9201f60baaf9b957dbc1b10d0efe6
6
+ metadata.gz: c4ed69be543e4865f580e149974f24b4ff78b91aa4ccef8a68646e60d1a797b2b81cdd94e202d5311125e08b8ca6b5f72f2a143daf39aa93fef88b705d367f5f
7
+ data.tar.gz: 2e4804f2911e02da1706709b8a8454eb38a31e158b3d7f1786a14a0d03ca40a21c521d2c5fa39b4dfa5d5fe38c611c435388ba9ddc94000928436edfc77fa57c
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_WITH: "development:test"
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 majid imanzade
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,54 @@
1
+ # Saoshyant
2
+
3
+ **Saoshyant is an exception handler Gem** that allows you to customize your exceptions to have a custom reaction when error raised in rails Application.
4
+
5
+ When you Encountering to an error in Rails Application it shows Red Page and explains Exception. With Saoshyant you can log or render error with custom status code.
6
+
7
+ the Most Usage of Saoshyant is in Rest Apis Applications that you need to render errors with custom error code and messages.
8
+
9
+ ## Requirements
10
+ * Rails: 3.0.0+
11
+
12
+ ## Installation
13
+ Add Saoshyant to your Gemfile:
14
+ ```sh
15
+ gem 'saoshyant'
16
+ ```
17
+
18
+ ## Getting Start
19
+
20
+ in blowe Example we want to render `Exceptions` messages in json format.
21
+
22
+ ```rb
23
+ class PageController < ApplicationController
24
+ include Saoshyant
25
+
26
+ def initialize
27
+ saoshyant do |status_code, msg, exception_klass|
28
+ render json: {message: msg, error_class_name: exception_klass}, status: status_code
29
+ end
30
+ end
31
+ end
32
+
33
+ ```
34
+
35
+ ### How Customize Status Code and Log
36
+ default status code in saoshyant gem is `500` error code and it doesnt log Exceptions.
37
+
38
+ if you want to customize Status Code and Log you can use `saoshyant_option` methode:
39
+ ```rb
40
+ saoshyant_option(ExceptionClass, StatusCode, LogFlag)
41
+ ```
42
+ ```rb
43
+ class PageController < ApplicationController
44
+ include Saoshyant
45
+
46
+ def initialize
47
+ saoshyant_option(NameError, 402, true)
48
+ saoshyant do |status_code, msg, exception_klass|
49
+ render json: {message: msg, error_class_name: exception_klass}, status: status_code
50
+ end
51
+ end
52
+ end
53
+
54
+ ```
@@ -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
@@ -1,20 +1,34 @@
1
1
  require 'rails'
2
2
  require 'exception_logger'
3
- require 'json_exception_handeler'
3
+ require 'exception_handler'
4
4
 
5
5
  module Saoshyant
6
6
  extend ActiveSupport::Concern
7
+ include ActiveSupport::Rescuable
8
+
9
+ @@exception_klasses = {}
10
+ @@block = nil
7
11
 
8
- DEFAULT_ERROR_CODE = 500
9
12
  included do
10
- 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
11
25
  end
12
26
 
13
- def render_exception ex
14
- code = Saoshyant::JsonExceptionHandeler.code_status ex
15
- log = Saoshyant::JsonExceptionHandeler.log_status ex
27
+ def execute_exception ex
28
+ code = Saoshyant::ExceptionHandler.code_status ex, @@exception_klasses
29
+ log = Saoshyant::ExceptionHandler.log_status ex, @@exception_klasses
16
30
 
17
31
  Saoshyant::ExceptionLogger.log(ex.message) if log == true
18
- render json: {status: 'error', :message => ex.message, :exception_type => ex.class.inspect}, status: code
32
+ @@block.call(code, ex.message, ex.class.inspect)
19
33
  end
20
34
  end
@@ -1,16 +1,18 @@
1
- # require 'version'
2
-
3
1
  Gem::Specification.new do |s|
4
2
  s.name = 'saoshyant'
5
- s.version = '1.0.1'
3
+ s.version = '1.2.1'
6
4
  s.date = '2020-02-10'
7
- s.summary = "Handle Rails Exception Apis"
8
- s.description = "when exceptoin raised we dont want see it in ugly and red display, saoshyant present it in json response"
5
+ s.summary = "Handle Rails Exception"
6
+ s.description = "Handle Unexpected Controller Exceptions using Saoshyant."
9
7
  s.authors = ["Majid Imanzade"]
10
8
  s.email = 'majidimanzade1@gmail.com'
11
9
  s.homepage = 'https://rubygems.org/gems/saoshyant'
10
+ s.homepage = "http://github.com/majidimanzade/saoshyant"
12
11
  s.license = 'MIT'
13
12
 
14
13
  s.files = `git ls-files`.split("\n")
15
14
  s.require_paths = ['lib']
15
+ s.add_development_dependency("bundler")
16
+ s.add_development_dependency("rake")
17
+ s.add_development_dependency("rspec")
16
18
  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: 1.0.1
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Majid Imanzade
@@ -9,23 +9,68 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2020-02-10 00:00:00.000000000 Z
12
- dependencies: []
13
- description: when exceptoin raised we dont want see it in ugly and red display, saoshyant
14
- present it in json response
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'
55
+ description: Handle Unexpected Controller Exceptions using Saoshyant.
15
56
  email: majidimanzade1@gmail.com
16
57
  executables: []
17
58
  extensions: []
18
59
  extra_rdoc_files: []
19
60
  files:
61
+ - ".bundle/config"
20
62
  - ".gitignore"
63
+ - ".rspec"
21
64
  - Gemfile
22
- - Rackfile
65
+ - LICENSE
66
+ - README.md
67
+ - lib/exception_handler.rb
23
68
  - lib/exception_logger.rb
24
- - lib/json_exception_handeler.rb
25
69
  - lib/saoshyant.rb
26
- - lib/version.rb
27
70
  - saoshyant.gemspec
28
- homepage: https://rubygems.org/gems/saoshyant
71
+ - spec/saoshyant_test.rb
72
+ - spec/spec_helper.rb
73
+ homepage: http://github.com/majidimanzade/saoshyant
29
74
  licenses:
30
75
  - MIT
31
76
  metadata: {}
@@ -47,5 +92,5 @@ requirements: []
47
92
  rubygems_version: 3.1.2
48
93
  signing_key:
49
94
  specification_version: 4
50
- summary: Handle Rails Exception Apis
95
+ summary: Handle Rails Exception
51
96
  test_files: []
data/Rackfile DELETED
@@ -1,8 +0,0 @@
1
- require 'rake/testtask'
2
-
3
- Rake::TestTask.new do |t|
4
- t.libs << 'test'
5
- end
6
-
7
- desc "Run tests"
8
- task :default => :test
@@ -1,27 +0,0 @@
1
- module Saoshyant
2
- class JsonExceptionHandeler
3
-
4
- @@exception_klasses = {}
5
-
6
- def self.customize(exception_klass, code, log = false)
7
- validate_arguments code, log
8
- return if @@exception_klasses.key?(exception_klass)
9
-
10
- @@exception_klasses.merge!(exception_klass => {code: code, log: log})
11
- end
12
-
13
- private
14
- def self.code_status exception
15
- @@exception_klasses.key?(exception.class) ? @@exception_klasses[exception.class][:code] : DEFAULT_ERROR_CODE
16
- end
17
-
18
- def self.log_status exception
19
- @@exception_klasses.key?(exception.class) ? @@exception_klasses[exception.class][:log] : false
20
- end
21
-
22
- def self.validate_arguments code, log
23
- raise "Invalid code status" unless code.kind_of?(Integer)
24
- raise "Invalid log status, it should be Boolean" unless !!log == log
25
- end
26
- end
27
- end
@@ -1,5 +0,0 @@
1
- module Saoshyant
2
- class Version
3
- GEM_VERSION = "0.0.1"
4
- end
5
- end