Solved

C# Error message formatting

Posted on 2014-09-11
3
44 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
ID: 40318551
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
ID: 40319082
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
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 video teaches viewers about errors in exception handling.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

777 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