Sinatra 기반 API를 작성 중이며 특정 끝점을 API 키로 보호하고 경로가 처리되기 전에 키의 유효성을 검사하려고합니다. 작동하지 않습니다 before
블록에서 오류를 던지는 이유 begin
/rescue
제표가 아직 호출되지 않았기 때문에이전 블록에서 JSON 응답을 반환하는 방법
나는 그러나 내가 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