carrierwave upload Encoding :: UndefinedConversionError:“\ xFF”,从ASCII-8BIT到UTF-8

时间:2023-01-05 20:44:14

Got this error on production but can't figure out where the problem is. Same request works fine on local development. But fail on production. I'm building API for mobile client use

生产中出现此错误但无法确定问题所在。相同的请求适用于本地开发。但生产失败了。我正在为移动客户端使用构建API

Error trace

```

Encoding::UndefinedConversionError: "\xFF" from ASCII-8BIT to UTF-8
... 39 non-project frames
File"/home/deploy/.bundler/example/ruby/2.0.0/gems/activesupport4.1.0/lib/active_support/core_ext/object/json.rb" line 34 in encode
File"/home/deploy/.bundler/example/ruby/2.0.0/gems/activesupport4.1.0/lib/active_support/core_ext/object/json.rb" line 34 in to_json
File"/home/deploy/.bundler/example/ruby/2.0.0/gems/activesupport4.1.0/lib/active_support/core_ext/object/json.rb" line 34 in to_json_with_active_support_encoder
File"/home/deploy/.bundler/example/ruby/2.0.0/gems/activesupport4.1.0/lib/active_support/json/encoding.rb" line 56 in to_json
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/json-1.8.1/lib/json/common.rb" line 223 in generate
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/json-1.8.1/lib/json/common.rb" line 223 in generate
File"/home/deploy/.bundler/example/ruby/2.0.0/gems/activesupport4.1.0/lib/active_support/json/encoding.rb" line 96 in stringify
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/activesupport4.1.0/lib/active_support/json/encoding.rb" line 34 in encode
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/activesupport-4.1.0/lib/active_support/json/encoding.rb" line 21 in encode
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/activesupport-4.1.0/lib/active_support/core_ext/object/json.rb" line 37 in to_json_with_active_support_encoder
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/multi_json-1.10.1/lib/multi_json/adapters/json_common.rb" line 21 in dump
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/multi_json-1.10.1/lib/multi_json/adapter.rb" line 24 in dump
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/multi_json-1.10.1/lib/multi_json.rb" line 136 in dump
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/rollbar-0.12.20/lib/rollbar.rb" line 437 in build_payload
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/rollbar-0.12.20/lib/rollbar.rb" line 95 in report_exception
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/rollbar-0.12.20/lib/rollbar/exception_reporter.rb" line 9 in report_exception_to_rollbar
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/rollbar-0.12.20/lib/rollbar/middleware/rails/show_exceptions.rb" line 22 in rescue in call_with_rollbar
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/rollbar-0.12.20/lib/rollbar/middleware/rails/show_exceptions.rb" line 19 in call_with_rollbar
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/show_exceptions.rb" line 30 in call
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/railties-4.1.0/lib/rails/rack/logger.rb" line 38 in call_app
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/railties-4.1.0/lib/rails/rack/logger.rb" line 20 in block in call
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/activesupport-4.1.0/lib/active_support/tagged_logging.rb" line 68 in block in tagged
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/activesupport-4.1.0/lib/active_support/tagged_logging.rb" line 26 in tagged
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/activesupport-4.1.0/lib/active_support/tagged_logging.rb" line 68 in tagged
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/railties-4.1.0/lib/rails/rack/logger.rb" line 20 in call
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/request_id.rb" line 21 in call
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/rack-1.5.2/lib/rack/methodoverride.rb" line 21 in call
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/rack-1.5.2/lib/rack/runtime.rb" line 17 in call
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/activesupport-4.1.0/lib/active_support/cache/strategy/local_cache_middleware.rb" line 26 in call
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/rack-1.5.2/lib/rack/sendfile.rb" line 112 in call
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/railties-4.1.0/lib/rails/engine.rb" line 514 in call
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/railties-4.1.0/lib/rails/application.rb" line 144 in call
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/unicorn-4.8.2/lib/unicorn/http_server.rb" line 572 in process_client
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/unicorn-4.8.2/lib/unicorn/http_server.rb" line 666 in worker_loop
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/unicorn-4.8.2/lib/unicorn/http_server.rb" line 521 in spawn_missing_workers
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/unicorn-4.8.2/lib/unicorn/http_server.rb" line 140 in start
File "/home/deploy/.bundler/example/ruby/2.0.0/gems/unicorn-4.8.2/bin/unicorn_rails" line 209 in <top (required)>
File "/home/deploy/.bundler/example/ruby/2.0.0/bin/unicorn_rails" line 23 in load
File "/home/deploy/.bundler/example/ruby/2.0.0/bin/unicorn_rails" line 23 in <main>

```

controller is pretty simple

```

  def create
    @work = current_user.works.build(permitted_params.work)
    if @work.save
      render json: @work, status: :created
    else
      render json: { status: 'error', message: @work.errors.full_messages }, status: :bad_request
    end
  end

```

raw request body

```

POST /api/my/works HTTP/1.1
Host: example.com
Cache-Control: no-cache
Postman-Token: 98f4ce29-a72e-53e9-98e6-d08bbc4ca6f1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryp7MA4YWxkTrZu0gW

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="auth_token"

0e008b699c8904c26cba54ac54a5bb49
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="name"

blah
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="cover_image"; filename="IMG_1755.JPG"
Content-Type: image/jpeg


----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="model"

iPad Air
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="work_type"

is_public
----WebKitFormBoundaryE19zNvXGzXaLvS5C

```

enviroments

Rails 4.1.1 Ruby 2.0

Rails 4.1.1 Ruby 2.0

2 个解决方案

#1


7  

According Rails 4.1 release note:

根据Rails 4.1发布说明:

Removed support for the encode_json hook used for encoding custom objects into JSON. This feature has been extracted into the activesupport-json_encoder gem. (Related Pull Request / More Details)

删除了对用于将自定义对象编码为JSON的encode_json挂钩的支持。此功能已提取到activesupport-json_encoder gem中。 (相关拉动请求/更多详情)

Because activesupport doesn't encoding invalid code anymore. Add activesupport-json_encoder gem to Gemfile fix this issue.

因为activesupport不再编码无效代码。将activesupport-json_encoder gem添加到Gemfile可以解决此问题。

#2


0  

Just adding if it can be of help to someone who is trying to find the solution to the error , while trying to write .jpg or any image files.

在尝试编写.jpg或任何图像文件时,只需添加它是否可以帮助那些试图找到错误解决方案的人。

"wb" instead of "w" in File.open(filename,'wb') has solved this issue for me.

File.open中的“wb”而不是“w”(filename,'wb')已经为我解决了这个问题。

#1


7  

According Rails 4.1 release note:

根据Rails 4.1发布说明:

Removed support for the encode_json hook used for encoding custom objects into JSON. This feature has been extracted into the activesupport-json_encoder gem. (Related Pull Request / More Details)

删除了对用于将自定义对象编码为JSON的encode_json挂钩的支持。此功能已提取到activesupport-json_encoder gem中。 (相关拉动请求/更多详情)

Because activesupport doesn't encoding invalid code anymore. Add activesupport-json_encoder gem to Gemfile fix this issue.

因为activesupport不再编码无效代码。将activesupport-json_encoder gem添加到Gemfile可以解决此问题。

#2


0  

Just adding if it can be of help to someone who is trying to find the solution to the error , while trying to write .jpg or any image files.

在尝试编写.jpg或任何图像文件时,只需添加它是否可以帮助那些试图找到错误解决方案的人。

"wb" instead of "w" in File.open(filename,'wb') has solved this issue for me.

File.open中的“wb”而不是“w”(filename,'wb')已经为我解决了这个问题。