Solved

C# Error message formatting

Posted on 2014-09-11
3
24 Views
Last Modified: 2016-06-21
we currently have a very convoluted methodology for displaying error messages.  But right now, I would just like to know how to correct one specific message.
The code used to build the message is:
        /// <summary>
        /// Adds the error details.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="errors">The errors.</param>
        private void AddErrorDetails(ref string message, Dictionary<string, string> errors)
        {
            if (errors == null)
            {
                return;
            }

            if (message == null)
            {
                message = string.Empty;
            }

            foreach (string key in errors.Keys)
            {
                if (!string.IsNullOrEmpty(message))
                {
                    message += Environment.NewLine;
                }

                if (key != null)
                {
                    message += errors[key];
                }
            }
        }

Open in new window


the resulting message looks like:

error-msg.png
I would like it to be more like:

Delivery succeeded but with errors. See below for details.
Adjusting stock:  NrfiInd=N, ParentMals UIC = 09107,  Niin = 007345641, Location - Staging......

you get the idea.
Any help would be appreciated.
0
Comment
Question by:PSCTECH
3 Comments
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
Comment Utility
Just to clarify, your question is about:
a) How to remove the curly braces regardless of their location?
b) How to remove extra key entry? e.g.  ParentMals={ParentMals UIC = 09107} to  ParentMals UIC = 09107
c) Are the extra keys well known values? (e.g. we can create a list so that we can replace them accordingly)

For (a), you just need to replace the braces with a blank space as follows:
message.Replace("{", " ").Replace("}", " ");

Open in new window



If more help is needed please post errors[key] contents and answer to my questions.
0
 

Author Comment

by:PSCTECH
Comment Utility
a. worked great, thank you.
b. yes that's how I want it to display
c.  no they are volatile and set at run time

I'm not at the office right now, I will try to get the errors[key] content as soon as I can
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now