saoshyant 1.0.1 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.bundle/config +2 -0
- data/.rspec +2 -0
- data/LICENSE +21 -0
- data/README.md +54 -0
- data/lib/exception_handler.rb +19 -0
- data/lib/saoshyant.rb +21 -7
- data/saoshyant.gemspec +7 -5
- data/spec/saoshyant_test.rb +14 -0
- data/spec/spec_helper.rb +4 -0
- metadata +54 -9
- data/Rackfile +0 -8
- data/lib/json_exception_handeler.rb +0 -27
- data/lib/version.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2148ce17d37d35084ad0b7a83ef4b556d2bf5de51e18cd3e8690f9edddaaf064
|
4
|
+
data.tar.gz: 3e53a5664c30efe33da7aa56c1583a3797374e6efcf6275cd06e674d5af68c00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4ed69be543e4865f580e149974f24b4ff78b91aa4ccef8a68646e60d1a797b2b81cdd94e202d5311125e08b8ca6b5f72f2a143daf39aa93fef88b705d367f5f
|
7
|
+
data.tar.gz: 2e4804f2911e02da1706709b8a8454eb38a31e158b3d7f1786a14a0d03ca40a21c521d2c5fa39b4dfa5d5fe38c611c435388ba9ddc94000928436edfc77fa57c
|
data/.bundle/config
ADDED
data/.rspec
ADDED
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.
|
data/README.md
ADDED
@@ -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
|
data/lib/saoshyant.rb
CHANGED
@@ -1,20 +1,34 @@
|
|
1
1
|
require 'rails'
|
2
2
|
require 'exception_logger'
|
3
|
-
require '
|
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: :
|
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
|
14
|
-
code = Saoshyant::
|
15
|
-
log = Saoshyant::
|
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
|
-
|
32
|
+
@@block.call(code, ex.message, ex.class.inspect)
|
19
33
|
end
|
20
34
|
end
|
data/saoshyant.gemspec
CHANGED
@@ -1,16 +1,18 @@
|
|
1
|
-
# require 'version'
|
2
|
-
|
3
1
|
Gem::Specification.new do |s|
|
4
2
|
s.name = 'saoshyant'
|
5
|
-
s.version = '1.
|
3
|
+
s.version = '1.2.1'
|
6
4
|
s.date = '2020-02-10'
|
7
|
-
s.summary = "Handle Rails Exception
|
8
|
-
s.description = "
|
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
|
data/spec/spec_helper.rb
ADDED
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.
|
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
|
-
|
14
|
-
|
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
|
-
-
|
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
|
-
|
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
|
95
|
+
summary: Handle Rails Exception
|
51
96
|
test_files: []
|
data/Rackfile
DELETED
@@ -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
|