Skip to content

文字コードが不正だった場合、500エラーを返してしまうが、400 bad requestとincorrect string valueを返した方がわかりやすい #30

Description

@harajune

#29 に関連して、下記のコードをテストしてみたところ500 internal server errorとなった。

curl -i "https://api.bocco.me/alpha/rooms/373f1516-3d0c-450a-b128-fd055339fd2d/messages" \
-d "access_token=5eae3f2bbc512ec93a6cb7069c7af4828be8d8b209ddd830d64e7a8c71e71aed" \
-d "unique_id=`uuidgen`" \
-d "media=text" \
-d "text=`echo こんにちはBOCCO!これはAPIテストです。|nkf -s`" \
-H "Accept-Language: ja"

サーバログを見ると下記が出力されており、データベースへのインサート時にwarningが出ており、このハンドルがうまくいっていないため500 internal server errorになってしまう。
warningなのでデータのインサートはから文字で成功してしまう。

» 18 Nov 2016 10:46:29.657 537 <190>1 2016-11-18T01:46:29.539018+00:00 app web.1 - - pid:4    01:46:29.538974 database.go:121: [SQL] measure#sql.insert=0.0182 insert into `messages` (`id`,`unique_id`,`user`,`room`,`created_at`,`updated_at`,`media`,`audio`,`image`,`deleted`,`_text`,`lang`,`message_type`,`detail_json`) values (null,?,?,?,?,?,?,?,?,?,?,?,?,?);; [xxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxx 101193 101185 2016-11-18 01:46:30 +0000 UTC 2016-11-18 01:46:30 +0000 UTC text false ����ɂ���BOCCO�I�����API�e�X�g�ł��B ja n ]
» 18 Nov 2016 10:46:29.657 228 <190>1 2016-11-18T01:46:29.539170+00:00 app web.1 - - pid:4    01:46:29.539142 messages.go:206: Warning 1366: Incorrect string value: '\x82\xB1\x82\xF1\x82\xC9...' for column '_text' at row 1

修正案としては下記。

  • warning: Invalid string valueがデータベースから返ってきた場合はインサートをrollbackし、400 bad requestを返却する。
  • エラー内容としてincorrect string valueを返す。

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions