Exception Handling

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
14 anos atrás
Hi,

I am new to NopCommerce and I like the functionality on the whole, but there seems to be very little exception handling in the code.

I have already managed to crash the Admin app several times just by entering data through the gui and today I have crashed the actual store by completing an order and then going back through the pages to the cart and changing the Qty, as it did not have an object instantiated at that time.

Am I going to have to go through the entire app adding exception handling?
or is there something I am missing?

Cheers
14 anos atrás
I've not experienced that.

Post the errors AND version you are using.

Hosted or Local?
14 anos atrás
I am using 1.4 and Visual Studio 2008 web server facility for development and testing.

Here are a few of the places I have crashed it.

The point being that there are very few try catch blocks in the code so any error not accounted for and handled in the code will end up displaying the catch all error page with all the details in it, which is not what an end user wants or should see.

Errors in the back end should be handled or passed up the chain with a more user friendly message.

There is such a lot of code in the Nop I don't relish the thought of going through it all adding try catches, which is what I am feeling ought to be done.

----------------------------------------------------------------------------------------------------------------------

protected global::NopSolutions.NopCommerce.Web.Administration.Modules.CategoryAddControl ctrlCategoryAdd;

<%@ Register TagPrefix="nopCommerce" TagName="CategoryInfo" Src="CategoryInfo.ascx" %>

Category


Error is not handled when user has added a new template with an incorrect templatePath
and then tries to select that template for a category


namespace NopSolutions.NopCommerce.Web
{
    public partial class CategoryPage : BaseNopPage
    {
        Category category = null;

        private void CreateChildControlsTree()
        {
            category = CategoryManager.GetCategoryByID(this.CategoryID);
            if (category != null)
            {
                Control child = null;

                CategoryTemplate categoryTemplate = category.CategoryTemplate;
                if (categoryTemplate == null)
                    throw new NopException(string.Format("Category template path can not be empty. CategoryID={0}", category.CategoryID));

                child = base.LoadControl(categoryTemplate.TemplatePath);
                this.CategoryPlaceHolder.Controls.Add(child);
            }
        }

-----------------------------------------------------------------------------------------------------------------

Can't remember what I was doing at this point but an exception occurred and was not handled.

namespace UrlRewritingNet.Configuration.Provider
{
    public class UrlRewritingProviderCollection : ProviderCollection
    {
        public override void Add(ProviderBase provider)
        {
            if (provider == null)
                throw new ArgumentNullException("provider");
            if (!(provider is UrlRewritingProvider))
            {
                string msg = string.Format("Provider must implement type {0}", typeof(UrlRewritingProvider).ToString());
                throw new ArgumentException(msg, "provider");
            }
            base.Add(provider);
        }

----------------------------------------------------------------------------------------------------------------------

I had completed an order but then went bcak through the pages and changed the qty in the cart page
and it crashed with a need a new .....

Exception handling not implemented.


        protected bool ValidateCart()
        {
            bool hasErrors = false;
            foreach (RepeaterItem item in rptShoppingCart.Items)
            {
                TextBox txtQuantity = item.FindControl("txtQuantity") as TextBox;
                Label lblShoppingCartItemID = item.FindControl("lblShoppingCartItemID") as Label;
                CheckBox cbRemoveFromCart = item.FindControl("cbRemoveFromCart") as CheckBox;
                Panel pnlWarnings = item.FindControl("pnlWarnings") as Panel;
                Label lblWarning = item.FindControl("lblWarning") as Label;

                int shoppingCartItemID = 0;
                int quantity = 0;
                if (txtQuantity != null && lblShoppingCartItemID != null && cbRemoveFromCart != null)
                {
                    int.TryParse(lblShoppingCartItemID.Text, out shoppingCartItemID);

                    if (!cbRemoveFromCart.Checked)
                    {
                        int.TryParse(txtQuantity.Text, out quantity);
                        ShoppingCartItem sci = ShoppingCartManager.GetShoppingCartItemByID(shoppingCartItemID);

                        List<string> warnings = ShoppingCartManager.GetShoppingCartItemWarnings(sci.ShoppingCartType,
                            sci.ProductVariantID, sci.AttributesXML, quantity);

                        if (warnings.Count > 0)
                        {
                            hasErrors = true;
                            if (pnlWarnings != null && lblWarning != null)
                            {
                                pnlWarnings.Visible = true;
                                lblWarning.Visible = true;

                                StringBuilder addToCartWarningsSb = new StringBuilder();
                                for (int i = 0; i < warnings.Count; i++)
                                {
                                    addToCartWarningsSb.Append(Server.HtmlEncode(warnings[i]));
                                    if (i != warnings.Count - 1)
                                    {
                                        addToCartWarningsSb.Append("<br />");
                                    }
                                }

                                lblWarning.Text = addToCartWarningsSb.ToString();
                            }
                        }
                    }
                }
            }
            return hasErrors;
        }

-----------------------------------------------------------------------------------------------------------
14 anos atrás
Wen you are adding or altering code or templates you'll have to either make sure it is correct or add your own exception blocks.
14 anos atrás
These examples are in the code downloaded in Version 1.4 not in my own code
14 anos atrás
Fidelia, you are seeing the exceptions because you are accessing the site via localhost (through VS). When a site is accessed remotely and an exception occurs, customers will be directed to ErrorPage.htm and the exception details will be logged to the database (Nop_Log table). You can view the log entries at Administration > System > Log.

.
14 anos atrás
I guess ideally in the example above the error would be outputted to the page so you knew what the error was straight away without having to go look in the logs.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.