C# driver. Nice exception message.

1,175 views
Skip to first unread message

Sebastian Stehle

unread,
Apr 29, 2015, 10:49:20 AM4/29/15
to mongod...@googlegroups.com
Hi,

I have some exception, but dont know where they come from. Usually I only write the exceptions to the log file and get nice error messages. But with the new driver I only get messages like this:

A write operation resulted in an error.
or
A bulk write operation resulted in one or more errors.

Not very helpful.I noticed that the exceptions have many properties like:

MongoWriteConcernException.WriteConcernResult.LastErrorMessage

or

MongoCommandException.ErrorMessage

or

MongoBulkWriteException.WriteConcernError.Message

or

MongoBulkWriteException.WriteErrors[Message]

what do I need to catch to get some nice and meaningful messages?

Craig Wilson

unread,
Apr 29, 2015, 11:24:10 AM4/29/15
to mongod...@googlegroups.com
We can certainly improve our error messaging. Here is the ticket for it: https://jira.mongodb.org/browse/CSHARP-1252.

Craig

Sebastian Stehle

unread,
Apr 29, 2015, 12:33:45 PM4/29/15
to mongod...@googlegroups.com
Do you also have a solution for now?

I am try to do something like this:

            try
            {
                // MAKE UPDATE
            }
            catch (MongoDuplicateKeyException ex)
            {
                ThrowDuplicateException(ex, EntityUpdateExceptionReason.UniqueFailed, ex.WriteConcernResult != null ? ex.WriteConcernResult.LastErrorMessage : "-");
            }
            catch (MongoWriteConcernException ex)
            {
                ThrowException(ex, EntityUpdateExceptionReason.Undefined, ex.WriteConcernResult != null ? ex.WriteConcernResult.LastErrorMessage : "-");
            }
            catch (MongoWriteException ex)
            {
                ThrowException(ex, EntityUpdateExceptionReason.Undefined, ex.WriteConcernError != null ? ex.WriteConcernError.Message : "-");
            }
            catch (MongoBulkWriteException ex)
            {
                string writeErrors = ex.WriteErrors != null ? string.Join(", ", ex.WriteErrors.Select(x => x.Message)) : "-";

                ThrowException(ex, EntityUpdateExceptionReason.Undefined, writeErrors);
            }
            catch (Exception ex)
            {
                throw new EntityUpdateException(EntityUpdateExceptionReason.Undefined, ex.Message, ex);
            }
        }

        private static void ThrowDuplicateException(Exception ex, EntityUpdateExceptionReason reason, string writeError)
        {
            string message = string.Format(CultureInfo.InvariantCulture, "An entity with the same unique constraints already exists. MongoDB Error: {0}, Exception message: {1}", writeError, ex.Message);

            throw new EntityUpdateException(reason, message, ex);
        }

        private static void ThrowException(Exception ex, EntityUpdateExceptionReason reason, string writeError)
        {
            string message = string.Format(CultureInfo.InvariantCulture, "Failed to update the entity. MongoDB Error: {0}, Exception message: {1}", writeError, ex.Message);

            throw new EntityUpdateException(reason, message, ex);
        }

But the Error message is often "-" because I dont know the right properties and exceptions to catch.

Sebastian Stehle

unread,
May 4, 2015, 11:31:10 AM5/4/15
to mongod...@googlegroups.com
Any idea how to get the best exception message? It is very critical for us. As I said the message is always empty in this way ("-")

Craig Wilson

unread,
May 4, 2015, 11:43:48 AM5/4/15
to mongod...@googlegroups.com
Hi Sebastien,

I pointed you at the ticket to make this better (CSHARP-1252). This is scheduled for 2.0.1. Once this is done, you should be able to catch a MongoException and have mostly what you need. Until then, you are going to need to do what you are doing and construct the message yourself.

However, there are number of runtime exceptions that might cause other kinds of exceptions. However, these should be caught during development (NotSupportException, etc...).

Craig

Sebastian Stehle

unread,
May 4, 2015, 1:25:54 PM5/4/15
to mongod...@googlegroups.com
I have no problem to catch all the exceptions myself, but I dont know, where I can find the detail exception. This is my problem. I need some advice here how to catch them correctly. It is not easy to test it and to generate all messages.

Craig Wilson

unread,
May 4, 2015, 1:56:38 PM5/4/15
to mongod...@googlegroups.com
It looked to me like you caught the pertinent ones. Could you perhaps elaborate on which exception types are getting thrown that are resulting in your empty message?

Sebastian Stehle

unread,
May 6, 2015, 6:31:38 AM5/6/15
to mongod...@googlegroups.com
Will try to find it out. I also recognized that the MongoDuplicateKeyException is not used in your driver. I always get a MongoWriteException. I would expect to get the MongoDuplicateKeyException in case I want to insert a document with a field, where a unique index exists for.

Craig Wilson

unread,
May 6, 2015, 7:43:49 AM5/6/15
to mongod...@googlegroups.com
You won't get a MongoDuplicateKeyException(it is there for backwards compat in the legacy API). There is a Category field in the WriteError that will indicate if it's a duplicate key. This makes handling the write errors consistent between single writes and bulk writes.

Craig
Reply all
Reply to author
Forward
0 new messages