私はSinatraベースのAPIを作成しており、APIキーを使用して特定のエンドポイントを保護し、ルートが処理される前にキーを検証します。前のブロックからJSON応答を返す方法
before
ブロックにエラーを投げが機能しない理由をbegin
/rescue
文はまだ呼び出されていないので、私はしかし、私はJSONレスポンスのようにエラーメッセージを表示して、クライアントに送信されるようにしたい、理解JSONオブジェクト
どうすればよいですか?
namespace '/v1/sponser/:key' do
before do
if APIHelper.valid_key?(params[:key]) == false
throw 'Error, invalid API key'
# is it possible to return a JSON response from the before statement here?
end
end
get '/test' do
begin
json status: 200, body: 'just a test'
rescue => error
json status: 404, error: error
end
end
end