Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
条带C#错误处理_C#_Asp.net_Stripe Payments_Stripe.net - Fatal编程技术网

条带C#错误处理

条带C#错误处理,c#,asp.net,stripe-payments,stripe.net,C#,Asp.net,Stripe Payments,Stripe.net,我设法使用Strip.net dll的一个版本来创建一个付款方法,但我在处理错误时遇到了问题。我做到了这一点 try { StripeCustomer current = GetCustomer(); // int? days = getaTraildays(); //if (days != null) //{ int chargetotal = 300; //Convert.ToInt32((3.33*Convert.ToInt32(days)*100)

我设法使用Strip.net dll的一个版本来创建一个付款方法,但我在处理错误时遇到了问题。我做到了这一点

try
{
    StripeCustomer current = GetCustomer();
    // int? days = getaTraildays();
    //if (days != null)
    //{
    int chargetotal = 300; //Convert.ToInt32((3.33*Convert.ToInt32(days)*100));
    var mycharge = new StripeChargeCreateOptions();
    mycharge.AmountInCents = chargetotal;
    mycharge.Currency = "USD";
    mycharge.CustomerId = current.Id;
    string key = "sk_test_XXX";
    var chargeservice = new StripeChargeService(key);
    StripeCharge currentcharge = chargeservice.Create(mycharge);
    //}
}        
catch (StripeException)
{
    lblerror.Text = "Please check your card information and try again";
}
它将捕获错误,并让用户知道存在问题,但我现在需要了解,如果流程正常,为什么它仍然显示错误。我知道这是一个问题的方式捕获是书面的,但我不确定如何处理和一切我已经尝试了这么多努力都失败了。我想做的是让它重定向到另一个页面。有什么想法吗

++更新

在Olivier Jacot Descombes的帮助下,我将代码更改为

catch (StripeException ex) 
{ 
lblerror.Text = (ex.Message); 
}

如果上面的评论完全回答了这一点,我们就能够获得更好的结果,但这里有更多关于这一点的信息:(特别感谢@tnw提供的毫无用处的评论)

有几种不同类型的错误需要以不同的方式处理。正如您在上面的链接中所看到的,存在api错误、无效请求错误和卡错误。您应该以不同的方式处理这三个问题,因为您可能不希望向用户显示api或内部错误

进入异常范围后,所需的信息将在exception.StripeError对象中。我不使用exception.HttpStatusCode和exception.StripeError对象,其外观如下:

public class StripeError
{
    [JsonProperty("type")]
    public string ErrorType { get; set; }

    [JsonProperty("message")]
    public string Message { get; set; }

    [JsonProperty("code")]
    public string Code { get; set; }

    [JsonProperty("param")]
    public string Parameter { get; set; }

    [JsonProperty("error")]
    public string Error { get; set; }

    [JsonProperty("error_description")]
    public string ErrorSubscription { get; set; }

    [JsonProperty("charge")]
    public string ChargeId { get; set; }
}
        catch (StripeException exception)
        {
            switch (exception.StripeError.ErrorType)
            {
                case "card_error":
                    //do some stuff, set your lblError or something like this
                    ModelState.AddModelError(exception.StripeError.Code, exception.StripeError.Message);

                    // or better yet, handle based on error code: exception.StripeError.Code

                    break;
                case "api_error":
                    //do some stuff
                    break;
                case "invalid_request_error":
                    //do some stuff
                    break;
                default:
                    throw;
            }
        }
        catch(Exception exception)
        { 
             etc...etc..
所以你会想要这样的东西:

public class StripeError
{
    [JsonProperty("type")]
    public string ErrorType { get; set; }

    [JsonProperty("message")]
    public string Message { get; set; }

    [JsonProperty("code")]
    public string Code { get; set; }

    [JsonProperty("param")]
    public string Parameter { get; set; }

    [JsonProperty("error")]
    public string Error { get; set; }

    [JsonProperty("error_description")]
    public string ErrorSubscription { get; set; }

    [JsonProperty("charge")]
    public string ChargeId { get; set; }
}
        catch (StripeException exception)
        {
            switch (exception.StripeError.ErrorType)
            {
                case "card_error":
                    //do some stuff, set your lblError or something like this
                    ModelState.AddModelError(exception.StripeError.Code, exception.StripeError.Message);

                    // or better yet, handle based on error code: exception.StripeError.Code

                    break;
                case "api_error":
                    //do some stuff
                    break;
                case "invalid_request_error":
                    //do some stuff
                    break;
                default:
                    throw;
            }
        }
        catch(Exception exception)
        { 
             etc...etc..
确保将StripeeException捕获放在第一位,否则会出现编译时错误


在card_错误案例中,您可能还希望根据发生的卡错误类型采取措施。其中有12个(请看上面的链接)-例如“card\u Decedden”或“invalid\u cvc”

您的“try”块中的代码是否可能引发不同的异常?否则(根据提供的小代码),应该捕获并抑制StripeeException。您的问题似乎是关于第三方API引发的异常。我建议你在那里查一下错误。我们不是该API的开发人员。在进入
try
块之前,您是否清除了
lblerror.Text
?异常被捕获。我的问题是,看起来所有消息都是作为异常抛出的,我想知道(我说的是否正确)。如果这不是一个会停止代码的异常,那么将用户重定向到另一个页面。可能吗?请尝试显示原始错误消息
catch(StripeException-ex){lblerror.Text=ex.message;}