api.rb 2.2 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

N
Nihad Abbasov 已提交
6
    rescue_from ActiveRecord::RecordNotFound do
7
      rack_response({ 'message' => '404 Not found' }.to_json, 404)
N
Nihad Abbasov 已提交
8 9
    end

C
Connor Shea 已提交
10 11 12 13
    # 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
14 15
    end

F
Felix Gilcher 已提交
16 17 18 19 20 21 22 23 24
    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 已提交
25
      API.logger.add Logger::FATAL, message
J
jubianchi 已提交
26
      rack_response({ 'message' => '500 Internal Server Error' }.to_json, 500)
27 28
    end

N
Nihad Abbasov 已提交
29
    format :json
30 31
    content_type :txt, "text/plain"

32 33 34
    # Ensure the namespace is right, otherwise we might load Grape::API::Helpers
    helpers ::API::Helpers

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