From 85fc2a3c6556df134ff0aa3e7420fd44eb8475f3 Mon Sep 17 00:00:00 2001 From: Maximiliano Garcia Roe Date: Thu, 16 Mar 2023 19:07:33 -0300 Subject: [PATCH] avoiding the use of the StandardError exception In general, it is better to rescue and display the specific error rather than showing a generic message in the view when working with Ruby. This is because a generic message may not provide enough information for the user to understand the problem and take steps to solve it. --- README.adoc | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/README.adoc b/README.adoc index f97d6cb..aec9d9f 100644 --- a/README.adoc +++ b/README.adoc @@ -337,6 +337,44 @@ render status: :forbidden ... ---- +=== Error Message [[http-status-code-symbols]] + +It is recommended to avoid using the StandardError exception when rendering error messages in the code, as this may capture an unintended and controlled error. +Instead, it is suggested to capture specific errors or send a customized standard error message. +This ensures more accurate and effective exception handling in the program. + +[source,ruby] +---- +# bad +... +rescue StandardError => e + render json: {error_msg: e.message} +end +... + +# bad - usign in flash message +... +rescue StandardError => e + flash[:error] = e.message +end +... + + +# good +... +rescue StandardError => e + render json: {error_msg: 'Error in the application'} +end +... + +# good +... +rescue ParticularError => e + render json: {error_msg: e.message} +end +... +---- + == Models === Model Classes [[model-classes]]