rails_react_errors 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d7ef71a095ca3fcaef5dc57a2add11dd13dea5c088226c4561957a93d72f7be5
4
+ data.tar.gz: 43401d67835cddd1ad89efeafefc0f91eb8b10671c36060832d5ec1cb5693d1c
5
+ SHA512:
6
+ metadata.gz: 5f032bd5f67b6c8938cc07a3d0d173cc973e2c834687829703e7402eda367bd0fa30f475d0b1994b0a3bdd58659a242fe2654bc1b8fa0dc584209bcffdba9543
7
+ data.tar.gz: 5c3b634d195ab8bc05be86366cfa5ab85d5fd7c9476dbfacc3c0f010ab3ce55cfcc2239da30d186b585f568314d123d1794a2c68289448b8c3c75ce7254227ae
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2026-03-06
4
+
5
+ - Initial release
@@ -0,0 +1,10 @@
1
+ # Code of Conduct
2
+
3
+ "rails_react_errors" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
4
+
5
+ * Participants will be tolerant of opposing views.
6
+ * Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
7
+ * When interpreting the words and actions of others, participants should always assume good intentions.
8
+ * Behaviour which can be reasonably considered harassment will not be tolerated.
9
+
10
+ If you have any concerns about behaviour within this project, please contact us at ["manish.prajapati0492@gmail.com"](mailto:"manish.prajapati0492@gmail.com").
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Manish0407
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # RailsReactErrors
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rails_react_errors`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails_react_errors. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/rails_react_errors/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the RailsReactErrors project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rails_react_errors/blob/master/CODE_OF_CONDUCT.md).
44
+
45
+ # rails_react_errors
46
+
47
+ Consistent error responses for Rails APIs used by React frontends.
48
+
49
+ ## Installation
50
+
51
+ ## Usage
52
+
53
+ ## Validation Errors
54
+
55
+ ## Not Found Errors
56
+
57
+ ## Unauthorized Errors
58
+
59
+ ## Configuration
60
+
61
+ ## React Example
62
+
63
+ ## Contributing
64
+
65
+ # rails_react_errors
66
+
67
+ Consistent error responses for Rails APIs used by React frontends.
68
+
69
+ ## Installation
70
+
71
+ Add this line to your application's Gemfile:
72
+
73
+ ```ruby
74
+ gem "rails_react_errors"
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsReactErrors
4
+ class Configuration
5
+ attr_accessor :include_full_messages,
6
+ :rescue_standard_error,
7
+ :log_errors
8
+
9
+ def initialize
10
+ @include_full_messages = true
11
+ @rescue_standard_error = false
12
+ @log_errors = true
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsReactErrors
4
+ module Controller
5
+ def self.included(base)
6
+ base.include RailsReactErrors::Renderer
7
+
8
+ if defined?(ActiveRecord::RecordNotFound)
9
+ base.rescue_from ActiveRecord::RecordNotFound, with: :handle_record_not_found
10
+ end
11
+
12
+ if defined?(ActionController::ParameterMissing)
13
+ base.rescue_from ActionController::ParameterMissing, with: :handle_parameter_missing
14
+ end
15
+
16
+ return unless RailsReactErrors.configuration.rescue_standard_error
17
+
18
+ base.rescue_from StandardError, with: :handle_internal_server_error
19
+ end
20
+
21
+ private
22
+
23
+ def handle_record_not_found(exception)
24
+ render_not_found_error(exception.message)
25
+ end
26
+
27
+ def handle_parameter_missing(exception)
28
+ render json: {
29
+ success: false,
30
+ message: exception.message,
31
+ code: 'PARAMETER_MISSING',
32
+ errors: {}
33
+ }, status: :bad_request
34
+ end
35
+
36
+ def handle_internal_server_error(exception)
37
+ log_exception(exception) if RailsReactErrors.configuration.log_errors
38
+ render_server_error('Something went wrong')
39
+ end
40
+
41
+ def log_exception(exception)
42
+ return unless defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
43
+
44
+ Rails.logger.error("[RailsReactErrors] #{exception.class}: #{exception.message}")
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/railtie'
4
+
5
+ module RailsReactErrors
6
+ class Railtie < Rails::Railtie
7
+ initializer 'rails_react_errors.configure_controller' do
8
+ ActiveSupport.on_load(:action_controller_api) do
9
+ include RailsReactErrors::Renderer
10
+ end
11
+
12
+ ActiveSupport.on_load(:action_controller) do
13
+ include RailsReactErrors::Renderer
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsReactErrors
4
+ module Renderer
5
+ def render_validation_error(record, status: :unprocessable_entity)
6
+ render json: RailsReactErrors::Serializer.validation(record), status: status
7
+ end
8
+
9
+ def render_not_found_error(message = 'Record not found', status: :not_found)
10
+ render json: RailsReactErrors::Serializer.not_found(message), status: status
11
+ end
12
+
13
+ def render_unauthorized_error(message = 'Unauthorized', status: :unauthorized)
14
+ render json: RailsReactErrors::Serializer.unauthorized(message), status: status
15
+ end
16
+
17
+ def render_forbidden_error(message = 'Forbidden', status: :forbidden)
18
+ render json: RailsReactErrors::Serializer.forbidden(message), status: status
19
+ end
20
+
21
+ def render_server_error(message = 'Something went wrong', status: :internal_server_error)
22
+ render json: RailsReactErrors::Serializer.server_error(message), status: status
23
+ end
24
+
25
+ def render_error(message:, code:, status:, errors: {})
26
+ render json: {
27
+ success: false,
28
+ message: message,
29
+ code: code,
30
+ errors: errors
31
+ }, status: status
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsReactErrors
4
+ class Serializer
5
+ def self.validation(record)
6
+ new.validation(record)
7
+ end
8
+
9
+ def self.not_found(message = 'Record not found')
10
+ new.not_found(message)
11
+ end
12
+
13
+ def self.unauthorized(message = 'Unauthorized')
14
+ new.unauthorized(message)
15
+ end
16
+
17
+ def self.forbidden(message = 'Forbidden')
18
+ new.forbidden(message)
19
+ end
20
+
21
+ def self.server_error(message = 'Something went wrong')
22
+ new.server_error(message)
23
+ end
24
+
25
+ def validation(record)
26
+ {
27
+ success: false,
28
+ message: 'Validation failed',
29
+ code: 'VALIDATION_ERROR',
30
+ errors: serialized_errors(record)
31
+ }
32
+ end
33
+
34
+ def not_found(message)
35
+ {
36
+ success: false,
37
+ message: message,
38
+ code: 'NOT_FOUND',
39
+ errors: {}
40
+ }
41
+ end
42
+
43
+ def unauthorized(message)
44
+ {
45
+ success: false,
46
+ message: message,
47
+ code: 'UNAUTHORIZED',
48
+ errors: {}
49
+ }
50
+ end
51
+
52
+ def forbidden(message)
53
+ {
54
+ success: false,
55
+ message: message,
56
+ code: 'FORBIDDEN',
57
+ errors: {}
58
+ }
59
+ end
60
+
61
+ def server_error(message)
62
+ {
63
+ success: false,
64
+ message: message,
65
+ code: 'INTERNAL_SERVER_ERROR',
66
+ errors: {}
67
+ }
68
+ end
69
+
70
+ private
71
+
72
+ def serialized_errors(record)
73
+ return {} unless record.respond_to?(:errors)
74
+
75
+ if RailsReactErrors.configuration.include_full_messages
76
+ record.errors.to_hash(true)
77
+ else
78
+ record.errors.to_hash
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsReactErrors
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'rails_react_errors/version'
4
+ require_relative 'rails_react_errors/configuration'
5
+ require_relative 'rails_react_errors/serializer'
6
+ require_relative 'rails_react_errors/renderer'
7
+ require_relative 'rails_react_errors/controller'
8
+
9
+ module RailsReactErrors
10
+ class Error < StandardError; end
11
+
12
+ class << self
13
+ attr_writer :configuration
14
+
15
+ def configuration
16
+ @configuration ||= Configuration.new
17
+ end
18
+
19
+ def configure
20
+ yield(configuration)
21
+ end
22
+ end
23
+ end
24
+
25
+ require_relative 'rails_react_errors/railtie' if defined?(Rails::Railtie)
@@ -0,0 +1,4 @@
1
+ module RailsReactErrors
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_react_errors
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Manish0407
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2026-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ description: rails_react_errors standardizes validation and API error responses in
28
+ a React-friendly JSON format.
29
+ email:
30
+ - manish.prajapati0492@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - CHANGELOG.md
36
+ - CODE_OF_CONDUCT.md
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - lib/rails_react_errors.rb
41
+ - lib/rails_react_errors/configuration.rb
42
+ - lib/rails_react_errors/controller.rb
43
+ - lib/rails_react_errors/railtie.rb
44
+ - lib/rails_react_errors/renderer.rb
45
+ - lib/rails_react_errors/serializer.rb
46
+ - lib/rails_react_errors/version.rb
47
+ - sig/rails_react_errors.rbs
48
+ homepage: https://github.com/Manish0407/rails_react_errors
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ allowed_push_host: https://rubygems.org
53
+ homepage_uri: https://github.com/Manish0407/rails_react_errors
54
+ source_code_uri: https://github.com/Manish0407/rails_react_errors
55
+ changelog_uri: https://github.com/Manish0407/rails_react_errors/blob/main/CHANGELOG.md
56
+ rubygems_mfa_required: 'true'
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 3.2.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.4.10
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Consistent error responses for Rails APIs used by React frontends
76
+ test_files: []