{"id":224,"date":"2015-12-04T08:00:55","date_gmt":"2015-12-03T23:00:55","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2015\/12\/04\/how-to-make-rack-middleware"},"modified":"2022-10-26T10:07:23","modified_gmt":"2022-10-26T01:07:23","slug":"how-to-make-rack-middleware","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2015\/12\/04\/how-to-make-rack-middleware\/","title":{"rendered":"10\u5206\u3067\u308f\u304b\u308b Rack \u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u306e\u4f5c\u308a\u65b9"},"content":{"rendered":"
\u5148\u9031\u304b\u3089\u4eca\u65e5\u307e\u30672\u9031\u9593\u30a2\u30e1\u30ea\u30ab\u65c5\u884c\u3092\u3057\u3066\u3044\u305f\u4f0a\u85e4\u3067\u3059\u3002
\n\u30b5\u30f3\u30d5\u30e9\u30f3\u30b7\u30b9\u30b3\u3067\u65e5\u672c\u6642\u9593\u306e\u30c1\u30fc\u30e0\u3068\u30ea\u30e2\u30fc\u30c8\u30ef\u30fc\u30af\u3092\u3057\u3066\u307f\u305f\u611f\u60f3\u306f\u5225\u306e\u8a18\u4e8b\u3067\u66f8\u3053\u3046\u3068\u601d\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n
\u5148\u65e5 hitode909<\/a> \u3055\u3093\u306e \u30ed\u30fc\u30ab\u30eb\u30b5\u30fc\u30d0\u30fc\u3067\u30a8\u30e9\u30fc\u304c\u51fa\u305f\u3089\u5373\u5ea7\u306bSlack\u306b\u6295\u7a3f\u3059\u308bPlack\u30df\u30c9\u30eb\u30a6\u30a7\u30a2<\/a> \u3068\u3044\u3046\u8a18\u4e8b\u3092\u62dd\u898b\u3057\u305f\u306e\u3067\u3059\u304c\u3001\u3061\u3087\u3046\u3069\u305d\u306e\u6642\u306b\u793e\u5185\u3067\u3082\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u30a8\u30f3\u30b8\u30cb\u30a2\u3068\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u30a8\u30f3\u30b8\u30cb\u30a2\u304c\u5bc6\u306a\u95a2\u4fc2\u3067\u958b\u767a\u3092\u9032\u3081\u3066\u3044\u305f\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u304c\u3042\u308a\u3001\u3053\u308c\u306f\u4fbf\u5229\u305d\u3046\u3060\u306d\uff01\u3068\u8a71\u984c\u306b\u4e0a\u308a\u307e\u3057\u305f\u3002 (2\u5206\u7d4c\u904e)<\/p>\n Rack \u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u3068\u306f\u4f55\u304b\u306b\u3064\u3044\u3066\u306f Ruby Freaks Lounge:\u7b2c25\u56de Rack\u3068\u306f\u4f55\u304b(3)\u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u306e\u3059\u3059\u3081<\/a> \u3067\u4e01\u5be7\u306b\u8aac\u660e\u304c\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u3053\u3061\u3089\u304c\u30aa\u30b9\u30b9\u30e1\u3067\u3059\u3002<\/p>\n \u3053\u306e\u8a18\u4e8b\u3092\u8aad\u3080\u306e\u306b\u5fc5\u8981\u306a\u6642\u9593\uff1a\u304a\u3088\u305d 3 \u5206<\/p>\n<\/blockquote>\n \u3068\u66f8\u304b\u308c\u3066\u3044\u308b\u306e\u3067\u30013\u5206\u3067\u8aad\u307f\u7d42\u308f\u3063\u3066\u304f\u3060\u3055\u3044\uff01<\/p>\n \u4e0a\u306e\u8a18\u4e8b\u306b\u3082\u7c21\u5358\u306b\u66f8\u304b\u308c\u3066\u3044\u307e\u3057\u305f\u304c\u3001Rack \u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u306e\u30df\u30cb\u30de\u30e0\u306a\u5f62\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n \u3053\u306e\u5f62\u3092\u5143\u306b\u3001\u3042\u3068\u306f\u81ea\u5206\u306e\u3084\u308a\u305f\u3044\u3088\u3046\u306a\u51e6\u7406\u3092\u52a0\u3048\u308b\u3060\u3051\u306a\u306e\u3067\u3001\u7c21\u5358\u3067\u3059\u306d\uff01 (7\u5206\u7d4c\u904e)<\/p>\n \u6b8b\u308a\u306e3\u5206\u3067\u3001\u4ed6\u306e\u8a18\u4e8b\u3067\u306f\u898b\u304b\u3051\u306a\u304b\u3063\u305f\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u306e\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n Airbrake<\/a> \u3068\u3044\u3046\u3001\u30a8\u30e9\u30fc\u30ed\u30b0\u3092\u898b\u3084\u3059\u3044\u5f62\u3067\u84c4\u7a4d\u3059\u308b\u30b5\u30fc\u30d3\u30b9\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u305d\u306egem<\/a> \u3082 Rack \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3068\u3057\u3066\u4f5c\u3089\u308c\u3066\u3044\u3066\u3001\u305d\u306e\u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u90e8\u5206(\u3053\u306e\u8fba\u308a<\/a>)\u3067\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u3092\u3057\u3066\u3044\u307e\u3059\u3002\u4ed6\u306b\u306f\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u898b\u3084\u3059\u3044\u5f62\u3067\u8868\u793a\u3059\u308b charliesome\/better_errors<\/a> \u3082\u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u90e8\u5206(\u3053\u306e\u8fba\u308a<\/a>)\u3067\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u3092\u3057\u3066\u3044\u305f\u306e\u3067\u3001\u305d\u306e\u5b9f\u88c5\u3092\u53c2\u8003\u306b\u3057\u307e\u3057\u305f\u3002 \u6700\u5f8c\u306b\u5c11\u3057\u6ce8\u610f\u3057\u305f\u3044\u306e\u304c\u3001\u4f8b\u3048\u3070 Rails \u3067 WebAPI \u3092\u4f5c\u308b\u6642\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306b Rails \u5074\u3067\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30eb\u3059\u308b\u5834\u5408\u3067\u3059\u3002\u3053\u306e\u3088\u3046\u306b\u3057\u3066\u3057\u307e\u3046\u3068 Rack \u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u307e\u3067 \u81ea\u5206\u3082\u307e\u3060 Rack \u306e\u89e6\u308a\u3060\u3051\u3057\u304b\u7406\u89e3\u3057\u3066\u3044\u306a\u3044\u306e\u3067\u3001\u3082\u3057\u9593\u9055\u3044\u304c\u3042\u308c\u3070 @showwin<\/a> \u307e\u3067\u3054\u9023\u7d61\u3044\u305f\u3060\u3051\u308c\u3070\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n (10\u5206\u7d4c\u904e!?)<\/p>\n
\n\u3068\u3044\u3046\u3053\u3068\u3067\u65e9\u901f Rails \u3067\u305d\u308c\u3068\u540c\u3058\u3088\u3046\u306a\u52d5\u4f5c\u3092\u3059\u308b Rack \u30df\u30c9\u30eb\u30a6\u30a7\u30a2 rack-error_to_slack<\/a> \u3092\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u3002\u6065\u305a\u304b\u3057\u3044\u3053\u3068\u306b\u4eca\u307e\u3067 Rack \u304c\u305d\u3082\u305d\u3082\u4f55\u306a\u306e\u304b\u3088\u304f\u308f\u304b\u3063\u3066\u3044\u307e\u305b\u3093\u3067\u3057\u305f\u304c\u3001\u5148\u4eba\u305f\u3061\u304c\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u4e0a\u306b\u77e5\u898b\u3092\u6b8b\u3057\u3066\u304a\u3044\u3066\u304f\u308c\u305f\u304a\u304b\u3052\u3067\u534a\u65e5\u3050\u3089\u3044\u3067\u4f5c\u6210\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002\u4eca\u56de\u306f\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u3092\u3059\u308b Rack \u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u306e\u4f5c\u6210\u65b9\u6cd5\u3092\u307e\u3068\u3081\u3066\u307f\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\nRack \u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u306e\u57fa\u672c<\/h2>\n
\n
class Foo\n def initialize(app)\n @app = app\n end\n\n def call(env)\n res = @app.call(env)\n # do something\n res\n end\nend<\/code><\/pre>\n
\n#do something<\/code> \u90e8\u5206\u306e\u5177\u4f53\u4f8b\u3092\u77e5\u308a\u305f\u3044\u65b9\u306f\u3001\u4ee5\u4e0b\u306e\u8a18\u4e8b\u306b\u7c21\u5358\u306a\u4f8b\u304c\u3042\u308a\u307e\u3057\u305f\u306e\u3067\u53c2\u8003\u306b\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n
\n
Rack \u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u3067\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0<\/h2>\n
\n\u3068\u3044\u3063\u3066\u3082\u96e3\u3057\u3044\u3053\u3068\u306f\u3057\u3066\u304a\u3089\u305a\u3001\u4eca\u56de\u4f5c\u6210\u3057\u305f rack-error_to_slack<\/code> \u3067\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30eb\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n
def call(env)\n @app.call(env)\nrescue Exception => exception\n Message.new(env, exception.message).send #Slack\u306b\u30e1\u30c3\u30bb\u30fc\u30b8\u9001\u4fe1\n raise exception\nend<\/code><\/pre>\n
Exception<\/code> \u304c\u6e21\u3055\u308c\u3066\u304d\u305f\u3089\u3001\u81ea\u5206\u306e\u884c\u3044\u305f\u3044\u51e6\u7406\u3092\u884c\u3063\u3066\u3001\u305d\u306e
Exception<\/code> \u3092\u6b21\u306e\u30ec\u30a4\u30e4\u30fc\u306b\u3082\u6e21\u3057\u3066\u3042\u3052\u308b\u3060\u3051\u3067\u3059\u3002<\/p>\n
Exception<\/code> \u304c\u6e21\u3055\u308c\u306a\u304f\u306a\u3063\u3066\u3057\u307e\u3046\u306e\u3067\u6c17\u3092\u3064\u3051\u307e\u3057\u3087\u3046\u3002<\/p>\n
class ApiController < ApplicationController\n rescue_from Exception, with: :render_500\n\n def render_500\n render json: { status: 500, message: 'Internal Server Error' }\n end\nend<\/code><\/pre>\n