mirror of
https://github.com/opf/openproject.git
synced 2026-06-14 03:30:14 +00:00
953ab1a6a8
This class got broken during what seems to be a
drive-by style-improvement in fbe1215365. That change:
* made it incompatible with frozen strings as error messages
* broke the intended hiding of messages if they came from the
wrong class
All of this went by unnoticed, because there were no specs
for the InternalError class.
Specs have now been added and the previous version of the code
mostly restored. Since there were some callers that always created the
exception with known safe error messages, I added a new class just for these
cases, because they were intended to "just show the message". So we can
keep using the original implementation for rescue_from handling.
67 lines
2.1 KiB
Ruby
67 lines
2.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
#-- copyright
|
|
# OpenProject is an open source project management software.
|
|
# Copyright (C) the OpenProject GmbH
|
|
#
|
|
# This program is free software; you can redistribute it and/or
|
|
# modify it under the terms of the GNU General Public License version 3.
|
|
#
|
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
|
# Copyright (C) 2010-2013 the ChiliProject Team
|
|
#
|
|
# This program is free software; you can redistribute it and/or
|
|
# modify it under the terms of the GNU General Public License
|
|
# as published by the Free Software Foundation; either version 2
|
|
# of the License, or (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, write to the Free Software
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
#
|
|
# See COPYRIGHT and LICENSE files for more details.
|
|
#++
|
|
|
|
module API
|
|
module Errors
|
|
# A representation for internal server errors that's safe to be used to wrap unexpected errors received in rescue_from.
|
|
# It will hide the detailed error message of some exception classes that are known to risk exposing internal details.
|
|
class InternalError < ErrorBase
|
|
identifier "InternalServerError"
|
|
code 500
|
|
|
|
def initialize(error_message = nil, exception:)
|
|
error = I18n.t("api_v3.errors.code_500")
|
|
|
|
if error_message && visible_exception?(exception)
|
|
error += " #{error_message}"
|
|
end
|
|
|
|
super(error)
|
|
end
|
|
|
|
private
|
|
|
|
##
|
|
# Hide internal database errors in production
|
|
def visible_exception?(exception)
|
|
exception_blocklist.none? do |clz|
|
|
exception.is_a?(clz)
|
|
end
|
|
end
|
|
|
|
def exception_blocklist
|
|
[
|
|
ActiveRecord::StatementInvalid
|
|
]
|
|
end
|
|
end
|
|
end
|
|
end
|