api_exception 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 470b587029958c92a73e72c11fa665682f6075b0a837a52458119c103b905f28
4
+ data.tar.gz: 1f397f8fb83fc90ee8b43c3ca5ce8ee305e4571d7676d5af543c786b7f2a7ec4
5
+ SHA512:
6
+ metadata.gz: 84588aebe63e702c03e127788d129e6105afd34c93f47648ee1223658399b85534892f43e08e427c899b1491395201981988247e2ce386f3a9c111d33e58ec75
7
+ data.tar.gz: b6d62c6f8b18bc816ef02063888a4f268093c088d2b95d7b98eb03b50c198f8f3b821a89d5d1f095b77d7c413021f43e8f7850ecfeb6a9f7e804505774cb749c
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright killjoy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # ApiException
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "api_exception"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install api_exception
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/setup"
2
+
3
+ load "rails/tasks/statistics.rake"
4
+
5
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/api_exception .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module ApiException
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ApiException
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ApiException
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module ApiException
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: "from@example.com"
4
+ layout "mailer"
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module ApiException
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Api exception</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= yield :head %>
9
+
10
+ <%= stylesheet_link_tag "api_exception/application", media: "all" %>
11
+ </head>
12
+ <body>
13
+
14
+ <%= yield %>
15
+
16
+ </body>
17
+ </html>
@@ -0,0 +1,18 @@
1
+ en:
2
+ base_exceptions:
3
+ record_not_found: "Record not found for %{model}"
4
+ timeout: "Could not connect to %{provider}"
5
+ invalid_user: Invalid user
6
+ duplicated_emails: Some user emails are duplicated
7
+ invalid_status_change: "Cannot change status from %{original_status} to %{new_status}"
8
+ unauthorized_user: User is unauthorized to do this action
9
+ standard_error: "%{message}"
10
+ cannot_update: "Cannot update %{model} with the requested parameters"
11
+ not_unique_record: ID should be unique for this datatype
12
+ action_not_permitted: Action is not permitted
13
+ invalid_file: One or more of the sent file are invalid or corrupt
14
+ unsupported_file: The file you are trying to upload is not supported
15
+ invalid_params: There was a problem in the JSON submitted
16
+ update_conflict: Update conflict. Please retry.
17
+ invalid_date_range: Invalid date range
18
+ authentication_failure: Verify authentication parameters.
@@ -0,0 +1,18 @@
1
+ es:
2
+ base_exceptions:
3
+ record_not_found: "Registro no encontrado para %{model}"
4
+ timeout: "No se pudo conectar a %{provider}"
5
+ invalid_user: Usuario invalido
6
+ duplicated_emails: Algunos e-mails están duplicados
7
+ invalid_status_change: "No se puede cambiar el status de %{original_status} a %{new_status}"
8
+ unauthorized_user: Usuario no autorizado para realizar esta acción
9
+ standard_error: "%{message}"
10
+ cannot_update: "No se puede actualizar %{model} con los estos parametros"
11
+ not_unique_record: El identificador debe ser único para este tipo de dato
12
+ action_not_permitted: La acción no está permitida
13
+ invalid_file: Uno o más de los archivos que se están enviando son inválidos o están corruptos
14
+ unsupported_file: El archivo que está intentando cargar no está soportado
15
+ invalid_params: Hubo un problema con el JSON enviado
16
+ update_conflict: Conflicto de actualización. Por favor, intenta nuevamente.
17
+ invalid_date_range: Rango de fechas inválido
18
+ authentication_failure: Verifique parámetros de autenticación.
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ ApiException::Engine.routes.draw do
2
+ end
@@ -0,0 +1,73 @@
1
+ require 'yaml'
2
+
3
+ module ApiException
4
+ class BaseException < StandardError
5
+ include ActiveModel::Model
6
+ include ActiveModel::Serialization
7
+
8
+ attr_reader :code, :message, :errors, :type, :error_type
9
+
10
+ def initialize(error_type, errors, params, platform = "API")
11
+ error = error_code_map[error_type]
12
+ @hash_message = error[:message]
13
+ @params = params
14
+
15
+ @error_type = error_type
16
+ @errors = [*errors]
17
+ @code = error[:code] if error
18
+ @message = parse_message if error
19
+ @errors.push(@message)
20
+ @errors = @errors.flatten
21
+ @type = platform
22
+ end
23
+
24
+ def parse_message
25
+ @parsed_message = @hash_message
26
+
27
+ if @params.present?
28
+ @params.each do |k, v|
29
+ param = @parsed_message["{{#{parse_key(k)}}}"] || @parsed_message["%{#{parse_key(k)}}"]
30
+ @parsed_message[param.to_s] = (v.to_s || "nil") if param
31
+ end
32
+ end
33
+
34
+ @parsed_message
35
+ end
36
+
37
+ private
38
+
39
+ def error_code_map
40
+ {
41
+ TIMEOUT: { code: 1, message: find_translation(:timeout) },
42
+ RECORD_NOT_FOUND: { code: 2, message: find_translation(:record_not_found) },
43
+ INVALID_USER: { code: 3, message: find_translation(:invalid_user) },
44
+ REPEATED_USER: { code: 4, message: find_translation(:duplicated_emails) },
45
+ INVALID_STATUS_CHANGE: { code: 5, message: find_translation(:invalid_status_change) },
46
+ UNAUTHORIZED: { code: 6, message: find_translation(:unauthorized_user) },
47
+ NOT_ALLOWED: { code: 7, message: find_translation(:standard_error) },
48
+ INVALID_UPDATE: { code: 8, message: find_translation(:cannot_update) },
49
+ BASE_ERROR: { code: 9, message: "" },
50
+ NOT_UNIQUE_RECORD: { code: 10, message: find_translation(:not_unique_record) },
51
+ ACTION_NOT_PERMITTED: { code: 11, message: find_translation(:action_not_permitted) },
52
+ INVALID_FILE: { code: 12, message: find_translation(:invalid_file) },
53
+ UNSUPPORTED_FILE: { code: 13, message: find_translation(:unsupported_file) },
54
+ INVALID_PARAMS: { code: 14, message: find_translation(:invalid_params) },
55
+ UPDATE_CONFLICT: { code: 15, message: find_translation(:update_conflict) },
56
+ INVALID_DATE_RANGE: { code: 16, message: find_translation(:invalid_date_range) },
57
+ AUTHENTICATION_FAILURE: { code: 17, message: find_translation(:authentication_failure) },
58
+ }
59
+ end
60
+
61
+ def parse_key(key)
62
+ if key.class == Integer
63
+ ""
64
+ else
65
+ key.try(:to_s)
66
+ end
67
+ end
68
+
69
+ def find_translation(error)
70
+ I18n.t("base_exceptions.#{error.to_s}")
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,5 @@
1
+ module ApiException
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace ApiException
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ module ApiException
2
+ # first number stable version
3
+ # second number major release
4
+ # bug fixes
5
+ VERSION = "1.0.0"
6
+ end
@@ -0,0 +1,15 @@
1
+ require "api_exception/version"
2
+ require "api_exception/engine"
3
+ require_relative 'api_exception/base_exception'
4
+
5
+ module ApiException
6
+ class << self
7
+ def root
8
+ Pathname.new(File.expand_path('../', __FILE__))
9
+ end
10
+
11
+ def setup
12
+ yield self
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :api_exception do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_exception
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - killjoy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.2.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.2.2
27
+ description: 'This gem pretends just be used for exception handling in a simple way,
28
+ giving to you: code, message and type error'
29
+ email:
30
+ - tiloanne96@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - MIT-LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - app/assets/config/api_exception_manifest.js
39
+ - app/assets/stylesheets/api_exception/application.css
40
+ - app/controllers/api_exception/application_controller.rb
41
+ - app/helpers/api_exception/application_helper.rb
42
+ - app/jobs/api_exception/application_job.rb
43
+ - app/mailers/api_exception/application_mailer.rb
44
+ - app/models/api_exception/application_record.rb
45
+ - app/views/layouts/api_exception/application.html.erb
46
+ - config/locales/en/en.yml
47
+ - config/locales/es/es.yml
48
+ - config/routes.rb
49
+ - lib/api_exception.rb
50
+ - lib/api_exception/base_exception.rb
51
+ - lib/api_exception/engine.rb
52
+ - lib/api_exception/version.rb
53
+ - lib/tasks/api_exception_tasks.rake
54
+ homepage: https://gitlab.com/code-nest2/api_exception
55
+ licenses:
56
+ - MIT
57
+ metadata:
58
+ homepage_uri: https://gitlab.com/code-nest2/api_exception
59
+ source_code_uri: https://gitlab.com/code-nest2/api_exception
60
+ changelog_uri: https://gitlab.com/code-nest2/api_exception/-/blob/main/README.md?ref_type=heads
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.5.21
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Simple Exception handle
80
+ test_files: []