mirror of
https://github.com/Retrospring/retrospring.git
synced 2024-11-20 16:29:52 +01:00
[skip ci] delocalize error reasons and add an API metrics configuration toggle
This commit is contained in:
parent
8b7b472cd3
commit
ef2d669447
8 changed files with 46 additions and 30 deletions
|
@ -50,8 +50,8 @@ class API < Grape::API
|
|||
next if CORS_SCHEME.index(uri.scheme).nil?
|
||||
|
||||
local_origin = "#{uri.scheme}://#{uri.host}"
|
||||
|
||||
if (uri.scheme == 'https' and uri.port != 443) or (uri.scheme == 'http' and uri.port != 80)
|
||||
|
||||
if (uri.scheme == 'https' and uri.port != 443) or (uri.scheme == 'http' and uri.port != 80)
|
||||
local_origin += ":#{uri.port}"
|
||||
end
|
||||
|
||||
|
@ -88,7 +88,9 @@ class API < Grape::API
|
|||
end
|
||||
end
|
||||
|
||||
use API::Metrics
|
||||
if APP_CONFIG["api"]["metrics"]
|
||||
use API::Metrics
|
||||
end
|
||||
|
||||
get do
|
||||
status 200
|
||||
|
@ -104,6 +106,6 @@ class API < Grape::API
|
|||
|
||||
route :any, '*path' do
|
||||
status 404
|
||||
{message: "Not found", status: 404}
|
||||
{success: false, status: 404, reason: "ERR_NOT_FOUND"}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -21,9 +21,15 @@ class ErrorHandler < Grape::Middleware::Base
|
|||
|
||||
payload = {
|
||||
message: message || e.message || options[:default_message] || "Unexpected error",
|
||||
status: status
|
||||
reason: "ERR_UNEXPECTED",
|
||||
status: status,
|
||||
success: false
|
||||
}
|
||||
|
||||
if status == 404
|
||||
payload[:reason] = "ERR_RECORD_NOT_FOUND"
|
||||
end
|
||||
|
||||
unless Rails.env.production?
|
||||
payload[:trace] = e.backtrace[0,10]
|
||||
payload[:exception] = e.inspect
|
||||
|
|
|
@ -11,7 +11,7 @@ class Sleipnir::AnswerAPI < Sleipnir::MountAPI
|
|||
answer = Answer.find params["id"]
|
||||
if answer.nil?
|
||||
status 404
|
||||
return present({success: false, code: 404, reason: "Cannot find answer"})
|
||||
return present({success: false, code: 404, reason: "ERR_USER_NOT_FOUND"})
|
||||
end
|
||||
represent answer, with: Sleipnir::Entities::AnswerEntity
|
||||
end
|
||||
|
@ -41,11 +41,11 @@ class Sleipnir::AnswerAPI < Sleipnir::MountAPI
|
|||
answer = Answer.find params[:id]
|
||||
if answer.nil?
|
||||
status 404
|
||||
return present({success: false, code: 404, reason: "Cannot find answer"})
|
||||
return present({success: false, code: 404, reason: "ERR_USER_NOT_FOUND"})
|
||||
end
|
||||
|
||||
current_user.comment(answer, params[:comment], current_application)
|
||||
present({success: true, code: 200, reason: "Commented on answer"})
|
||||
present({success: true, code: 200, reason: "SUCCESS_COMMENT"})
|
||||
end
|
||||
|
||||
desc "Delete comment"
|
||||
|
|
|
@ -11,7 +11,7 @@ class Sleipnir::QuestionAPI < Sleipnir::MountAPI
|
|||
question = Question.find params["id"]
|
||||
if question.nil?
|
||||
status 404
|
||||
return present({success: false, code: 404, reason: "Cannot find question"})
|
||||
return present({success: false, code: 404, reason: "ERR_QUESTION_NOT_FOUND"})
|
||||
end
|
||||
represent question, with: Sleipnir::Entities::QuestionEntity
|
||||
end
|
||||
|
@ -31,13 +31,13 @@ class Sleipnir::QuestionAPI < Sleipnir::MountAPI
|
|||
question = Question.find params["id"]
|
||||
if question.nil?
|
||||
status 404
|
||||
return present({success: false, code: 404, reason: "Cannot find question"})
|
||||
return present({success: false, code: 404, reason: "ERR_QUESTION_NOT_FOUND"})
|
||||
end
|
||||
|
||||
inbox = Inbox.find_by question: question
|
||||
if inbox.nil? and not question.user.privacy_allow_stranger_questions
|
||||
status 300
|
||||
return present({success: false, code: 300, reason: "User doesn't allow strangers to answer their questions"})
|
||||
status 403
|
||||
return present({success: false, code: 403, reason: "ERR_MUST_FOLLOW"})
|
||||
end
|
||||
|
||||
answer = if inbox.nil?
|
||||
|
@ -53,7 +53,7 @@ class Sleipnir::QuestionAPI < Sleipnir::MountAPI
|
|||
|
||||
ShareWorker.perform_async(current_user.id, answer.id, services)
|
||||
|
||||
present({success: true, code: 200, reason: "Answered question ##{params[:id]}"})
|
||||
present({success: true, code: 200, reason: "SUCCESS_ANSWERED"})
|
||||
end
|
||||
|
||||
desc "Given question's answers"
|
||||
|
|
|
@ -56,12 +56,12 @@ class Sleipnir::UserAPI < Sleipnir::MountAPI
|
|||
target = User.find params[:id]
|
||||
if target.nil?
|
||||
status 404
|
||||
return present({success: false, code: 404, reason: "Cannot find user"})
|
||||
return present({success: false, code: 404, reason: "ERR_USER_NOT_FOUND"})
|
||||
end
|
||||
|
||||
if params[:anonymous] and not target.privacy_allow_anonymous_questions
|
||||
status 300
|
||||
return present({success: false, code: 300, reason: "User doesn't allow anonymous questions"})
|
||||
status 403
|
||||
return present({success: false, code: 403, reason: "ERR_USER_NO_ANONY"})
|
||||
end
|
||||
|
||||
question = Question.create!(content: params[:question],
|
||||
|
@ -75,7 +75,7 @@ class Sleipnir::UserAPI < Sleipnir::MountAPI
|
|||
|
||||
Inbox.create!(user: target, question: question, new: true)
|
||||
|
||||
present({success: true, code: 200, reason: "Asked user \##{params[:id]} a question"})
|
||||
present({success: true, code: 200, reason: "SUCCESS_ASKED"})
|
||||
end
|
||||
|
||||
desc "Given user's answers"
|
||||
|
@ -115,16 +115,14 @@ class Sleipnir::UserAPI < Sleipnir::MountAPI
|
|||
author_is_anonymous: params[:anonymous],
|
||||
user: current_user)
|
||||
|
||||
unless current_user.nil?
|
||||
current_user.increment! :asked_count unless params[:anonymous]
|
||||
end
|
||||
current_user.increment! :asked_count unless params[:anonymous]
|
||||
|
||||
current_user.followers.each do |f|
|
||||
next if params[:anonymous] and not f.privacy_allow_anonymous_questions
|
||||
Inbox.create!(user: f, question: question, new: true)
|
||||
end
|
||||
|
||||
present({success: true, code: 200, reason: "Asked all your followers a question"})
|
||||
present({success: true, code: 200, reason: "SUCCESS_ASKED_ALL"})
|
||||
end
|
||||
|
||||
desc "Follow given user"
|
||||
|
@ -135,13 +133,13 @@ class Sleipnir::UserAPI < Sleipnir::MountAPI
|
|||
user = User.find(params[:id])
|
||||
if user.nil?
|
||||
status 404
|
||||
return present({success: false, code: 404, reason: "Cannot find user #{params[:id]}"})
|
||||
return present({success: false, code: 404, reason: "ERR_USER_NOT_FOUND"})
|
||||
end
|
||||
current_user.follow(user)
|
||||
present({success: true, code: 200, reason: "Followed user #{params[:id]}"})
|
||||
present({success: true, code: 200, reason: "SUCCESS_FOLLOWED"})
|
||||
rescue
|
||||
status 403
|
||||
present({success: false, code: 503, reason: "Already following user"})
|
||||
present({success: false, code: 503, reason: "ERR_ALREADY_FOLLOWING"})
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -153,13 +151,13 @@ class Sleipnir::UserAPI < Sleipnir::MountAPI
|
|||
user = User.find(params[:id])
|
||||
if user.nil?
|
||||
status 404
|
||||
return present({success: false, code: 404, reason: "Cannot find user #{params[:id]}"})
|
||||
return present({success: false, code: 404, reason: "ERR_USER_NOT_FOUND"})
|
||||
end
|
||||
current_user.unfollow(user)
|
||||
present({success: true, code: 200, reason: "Unfollowed user #{params[:id]}"})
|
||||
present({success: true, code: 200, reason: "SUCCESS_UNFOLLOWED"})
|
||||
rescue
|
||||
status 403
|
||||
present({success: false, code: 403, reason: "Not following user"})
|
||||
present({success: false, code: 403, reason: "ERR_NOT_FOLLOWING"})
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -30,7 +30,12 @@ class Oauth::ApplicationsController < Doorkeeper::ApplicationsController
|
|||
end
|
||||
|
||||
def metrics
|
||||
payload = ApplicationMetric.request(params["id"])
|
||||
payload = if APP_CONFIG["api"]["metrics"]
|
||||
ApplicationMetric.request(params["id"])
|
||||
else
|
||||
[]
|
||||
end
|
||||
|
||||
render json: payload
|
||||
end
|
||||
|
||||
|
|
|
@ -30,8 +30,9 @@
|
|||
%code= uri
|
||||
%td
|
||||
%a.btn.btn-warning.btn-expand{href: oauth_authorization_path(client_id: @application.uid, redirect_uri: uri, response_type: 'code', scopes: @application.scopes)}= t('doorkeeper.applications.buttons.authorize')
|
||||
%h4= t('.metrics')
|
||||
%div{data: {metrics: metrics_oauth_application_path(id: @application.id)}}
|
||||
- if APP_CONFIG["api"]["metrics"]
|
||||
%h4= t('.metrics')
|
||||
%div{data: {metrics: metrics_oauth_application_path(id: @application.id)}}
|
||||
.col-md-4.app--actions
|
||||
%h1= t('.actions')
|
||||
%p
|
||||
|
|
|
@ -55,3 +55,7 @@ fog:
|
|||
directory: 'retrospring'
|
||||
# URL host, comment out to use default, GENERALLY you don't want to define this
|
||||
# host: ''
|
||||
|
||||
# api settings
|
||||
api:
|
||||
metrics: true # WARNING: might be resource intensive, has complex SQL queries
|
||||
|
|
Loading…
Reference in a new issue