public bool TryTranslateMessage(Exception ex, out RESTError webServiceError)
        {
            webServiceError = null;

            var exception = ex is GenericADOException
                ? ex.InnerException
                : ex;

            if (exception is PostgresException postgresException)
            {
                var match = _expression.Match(postgresException.Message);

                if (match.Success)
                {
                    var exceptionInfo = new PostgresExceptionInfo(postgresException, _detailExpression);

                    var handledMessage = GetHandledMessage(exceptionInfo, match);

                    webServiceError = new RESTError
                    {
                        Code    = (int)HttpStatusCode.Conflict,
                        Type    = "Conflict",
                        Message = handledMessage
                    };

                    return(true);
                }
            }

            return(false);
        }
예제 #2
0
        public bool TryTranslateMessage(Exception ex, out RESTError webServiceError)
        {
            webServiceError = null;

            var exception = ex is GenericADOException
                ? ex.InnerException
                : ex;

            if (exception is PostgresException postgresException)
            {
                var match = _expression.Match(postgresException.Message);

                if (match.Success)
                {
                    var exceptionInfo = new PostgresExceptionInfo(postgresException, _detailExpression);

                    string message = string.Format(exceptionInfo.IsComposedKeyConstraint
                        ? ComposedKeyMessageFormat
                        : SimpleKeyMessageFormat, exceptionInfo.Values, exceptionInfo.ColumnNames, exceptionInfo.TableName);

                    webServiceError = new RESTError
                    {
                        Code    = (int)HttpStatusCode.Conflict,
                        Type    = "Conflict",
                        Message = message
                    };

                    return(true);
                }
            }

            return(false);
        }
        private static string GetHandledMessageFromExceptionMessage(PostgresExceptionInfo exceptionInfo, Match exceptionMessageMatch)
        {
            var constraintTypeText = exceptionMessageMatch.Groups["ConstraintType"].Value;
            var tableName          = exceptionMessageMatch.Groups["TableName"].Value;

            return(constraintTypeText.Equals("insert or update")
                ? string.Format(InsertOrUpdateMessageFormat, tableName)
                : string.Format(UpdateOrDeleteMessageFormat, tableName, exceptionInfo.ColumnNames));
        }
        private static string GetHandledMessage(PostgresExceptionInfo exceptionInfo, Match exceptionMessageMatch)
        {
            switch (exceptionInfo.ConstraintType)
            {
            case PostgresExceptionConstraintType.InsertOrUpdateForeignKey:
                return(string.Format(InsertOrUpdateMessageFormat, exceptionInfo.TableName));

            case PostgresExceptionConstraintType.UpdateOrDeleteForeignKey:
                return(string.Format(UpdateOrDeleteMessageFormat, exceptionInfo.TableName, exceptionInfo.ColumnNames));

            default:
                return(GetHandledMessageFromExceptionMessage(exceptionInfo, exceptionMessageMatch));
            }
        }