api.rb 2.4 KB
Newer Older
1
module API
N
Nihad Abbasov 已提交
2
  class API < Grape::API
V
Valery Sizov 已提交
3
    include APIGuard
R
Riyad Preukschas 已提交
4
    version 'v3', using: :path
N
Nihad Abbasov 已提交
5

6 7 8 9
    rescue_from Gitlab::Access::AccessDeniedError do
      rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
    end

N
Nihad Abbasov 已提交
10
    rescue_from ActiveRecord::RecordNotFound do
11
      rack_response({ 'message' => '404 Not found' }.to_json, 404)
N
Nihad Abbasov 已提交
12 13
    end

C
Connor Shea 已提交
14 15 16 17
    # Retain 405 error rather than a 500 error for Grape 0.15.0+.
    # See: https://github.com/ruby-grape/grape/commit/252bfd27c320466ec3c0751812cf44245e97e5de
    rescue_from Grape::Exceptions::Base do |e|
      error! e.message, e.status, e.headers
18 19
    end

F
Felix Gilcher 已提交
20 21 22 23 24 25 26 27 28
    rescue_from :all do |exception|
      # lifted from https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb#L60
      # why is this not wrapped in something reusable?
      trace = exception.backtrace

      message = "\n#{exception.class} (#{exception.message}):\n"
      message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code)
      message << "  " << trace.join("\n  ")

A
Andrey Kumanyaev 已提交
29
      API.logger.add Logger::FATAL, message
J
jubianchi 已提交
30
      rack_response({ 'message' => '500 Internal Server Error' }.to_json, 500)
31 32
    end

N
Nihad Abbasov 已提交
33
    format :json
34 35
    content_type :txt, "text/plain"

36 37 38
    # Ensure the namespace is right, otherwise we might load Grape::API::Helpers
    helpers ::API::Helpers

39
    mount ::API::AccessRequests
40 41 42 43 44 45
    mount ::API::AwardEmoji
    mount ::API::Branches
    mount ::API::Builds
    mount ::API::CommitStatuses
    mount ::API::Commits
    mount ::API::DeployKeys
Z
Z.J. van de Weg 已提交
46
    mount ::API::Deployments
47
    mount ::API::Environments
48 49 50
    mount ::API::Files
    mount ::API::Groups
    mount ::API::Internal
51
    mount ::API::Issues
52 53
    mount ::API::Keys
    mount ::API::Labels
54
    mount ::API::LicenseTemplates
55
    mount ::API::Members
56
    mount ::API::MergeRequests
57 58
    mount ::API::Milestones
    mount ::API::Namespaces
59
    mount ::API::Notes
Z
Z.J. van de Weg 已提交
60
    mount ::API::Pipelines
61
    mount ::API::ProjectHooks
62 63 64 65
    mount ::API::ProjectSnippets
    mount ::API::Projects
    mount ::API::Repositories
    mount ::API::Runners
66
    mount ::API::Services
67
    mount ::API::Session
68
    mount ::API::Settings
69 70 71
    mount ::API::SidekiqMetrics
    mount ::API::Subscriptions
    mount ::API::SystemHooks
72
    mount ::API::Tags
Z
ZJ van de Weg 已提交
73
    mount ::API::Templates
D
Douglas Barbosa Alexandre 已提交
74
    mount ::API::Todos
75
    mount ::API::Triggers
76
    mount ::API::Users
77
    mount ::API::Variables
N
Nihad Abbasov 已提交
78
  end
79
end