rack-autocrud 0.1.3 → 0.1.4

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.
Files changed (2) hide show
  1. data/lib/rack/autocrud.rb +19 -4
  2. metadata +2 -2
data/lib/rack/autocrud.rb CHANGED
@@ -74,9 +74,17 @@ module Rack
74
74
  return ret unless ret.nil?
75
75
  end
76
76
 
77
+ # Rewind the body
77
78
  request.body.rewind if request.body.respond_to?(:rewind)
78
- obj = model.create(JSON.parse(request.body.read))
79
- halt [402, '{ "error": "Failed to save ' + endpoint + '" }'] unless obj && obj.saved?
79
+
80
+ # Attempt to create the model object
81
+ obj = nil
82
+ begin
83
+ obj = model.create(JSON.parse(request.body.read))
84
+ halt [402, '{ "error": "Failed to save ' + endpoint + '" }'] unless obj && obj.saved?
85
+ rescue JSON::ParserError
86
+ return [ 400, { 'error' => 'Invalid JSON in request body.', 'details' => $! }.to_json ]
87
+ end
80
88
 
81
89
  # Call the post-create hook
82
90
  if self.respond_to?(:post_create)
@@ -112,9 +120,16 @@ module Rack
112
120
  return ret unless ret.nil?
113
121
  end
114
122
 
123
+ # Rewind the body
115
124
  request.body.rewind if request.body.respond_to?(:rewind)
116
- saved = model.update(JSON.parse(request.body.read))
117
- halt [403, 'Access Denied'] unless saved
125
+
126
+ # Attempt to update the model
127
+ begin
128
+ saved = model.update(JSON.parse(request.body.read))
129
+ halt [403, 'Access Denied'] unless saved
130
+ rescue JSON::ParserError
131
+ return [ 400, { 'error' => 'Invalid JSON in request body.', 'details' => $! }.to_json ]
132
+ end
118
133
 
119
134
  # Call the post-update hook
120
135
  if self.respond_to?(:post_update)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-autocrud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-28 00:00:00.000000000 Z
12
+ date: 2012-10-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json