public SimpleError GenerateSimpleError(int maxErrorNestingLevel = 3, bool generateData = true, int currentNestingLevel = 0)
        {
            var error = new SimpleError {
                Message = @"Generated exception message.", Type = ExceptionTypes.Random()
            };

            if (generateData)
            {
                for (int i = 0; i < RandomData.GetInt(1, 5); i++)
                {
                    string key = RandomData.GetWord();
                    while (error.Data.ContainsKey(key) || key == Event.KnownDataKeys.Error)
                    {
                        key = RandomData.GetWord();
                    }

                    error.Data.Add(key, RandomData.GetString());
                }
            }

            error.StackTrace = RandomData.GetString();

            if (currentNestingLevel < maxErrorNestingLevel && RandomData.GetBool())
            {
                error.Inner = GenerateSimpleError(maxErrorNestingLevel, generateData, currentNestingLevel + 1);
            }

            return(error);
        }
Exemple #2
0
        public override async Task EventProcessingAsync(EventContext context)
        {
            if (!context.Event.IsError())
            {
                return;
            }

            SimpleError error = context.Event.GetSimpleError();

            if (error == null)
            {
                return;
            }

            if (String.IsNullOrWhiteSpace(context.Event.Message))
            {
                context.Event.Message = error.Message;
            }

            // TODO: Parse the stack trace and upgrade this to a full error.
            if (!String.IsNullOrEmpty(error.Type))
            {
                context.StackSignatureData.Add("ExceptionType", error.Type);
            }

            if (!String.IsNullOrEmpty(error.StackTrace))
            {
                context.StackSignatureData.Add("StackTrace", error.StackTrace.ToSHA1());
            }
        }
Exemple #3
0
        public static SimpleError ToSimpleErrorModel(this Exception exception)
        {
            Type type = exception.GetType();

            var error = new SimpleError {
                Message    = GetMessage(exception),
                Type       = type.FullName,
                StackTrace = exception.StackTrace
            };

            // TODO: Test adding non-serializable objects to ExtendedData and see what happens
            try {
                Dictionary <string, object> extraProperties = type.GetPublicProperties().Where(p => !_exceptionExclusions.Contains(p.Name)).ToDictionary(p => p.Name, p => {
                    try {
                        return(p.GetValue(exception, null));
                    } catch {}
                    return(null);
                });

                extraProperties = extraProperties.Where(kvp => !ValueIsEmpty(kvp.Value)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                if (extraProperties.Count > 0 && !error.Data.ContainsKey(SimpleError.KnownDataKeys.ExtraProperties))
                {
                    error.AddObject(new ExtendedDataInfo {
                        Data = extraProperties,
                        Name = SimpleError.KnownDataKeys.ExtraProperties,
                        IgnoreSerializationErrors = true,
                        MaxDepthToSerialize       = 5
                    });
                }
            } catch {}

            if (exception.InnerException != null)
            {
                error.Inner = exception.InnerException.ToSimpleErrorModel();
            }

            return(error);
        }
        public override Task EventProcessingAsync(EventContext context)
        {
            if (!context.Event.IsError())
            {
                return(Task.CompletedTask);
            }

            SimpleError error = context.Event.GetSimpleError();

            if (error == null)
            {
                return(Task.CompletedTask);
            }

            if (String.IsNullOrWhiteSpace(context.Event.Message))
            {
                context.Event.Message = error.Message;
            }

            if (context.StackSignatureData.Count > 0)
            {
                return(Task.CompletedTask);
            }

            // TODO: Parse the stack trace and upgrade this to a full error.
            if (!String.IsNullOrEmpty(error.Type))
            {
                context.StackSignatureData.Add("ExceptionType", error.Type);
            }

            if (!String.IsNullOrEmpty(error.StackTrace))
            {
                context.StackSignatureData.Add("StackTrace", error.StackTrace.ToSHA1());
            }

            error.Data[Error.KnownDataKeys.TargetInfo] = new SettingsDictionary(context.StackSignatureData);
            return(Task.CompletedTask);
        }
        public override void EventProcessing(EventContext context)
        {
            if (!context.Event.IsError())
            {
                return;
            }

            SimpleError error = context.Event.GetSimpleError();

            if (error == null)
            {
                return;
            }

            if (String.IsNullOrWhiteSpace(context.Event.Message))
            {
                context.Event.Message = error.Message;
            }

            // TODO: Parse the stack trace and run it through the ErrorSignature.
            context.StackSignatureData.Add("ExceptionType", error.Type);
            context.StackSignatureData.Add("StackTrace", error.StackTrace.ToSHA1());
        }
Exemple #6
0
        public async Task <IActionResult> Link(RedirectItem newLink)
        {
            string apiKey = HttpContext.Request.Headers[header_apiKey];

            logger.Info("API POST /link - Request using APY key " + apiKey, newLink);

            if (string.IsNullOrWhiteSpace(apiKey))
            {
                var e = new SimpleError("API Key is missing");
                logger.Error("API POST /link", e);
                return(BadRequest(e));
            }
            else if (string.IsNullOrWhiteSpace(newLink.URL))
            {
                var e = new SimpleError("url cannot be blank.");
                logger.Error("API POST /link", e);
                return(BadRequest());
            }

            try
            {
                DeveloperApplicationDTO app = applications.GetByApiKey(apiKey);
                if (app != null)
                {
                    bool isSafe = await _sba.CheckUrl(newLink.URL);

                    if (!isSafe)
                    {
                        app.UnsafeURLSubmissions++;
                        applications.Update(app);
                        logger.Info("API POST /link - unsafe URL");
                        return(BadRequest(new SimpleError("This URL has been marked as unsafe and cannot be added")));
                    }

                    newLink.DateAdded              = DateTime.Now;
                    newLink.TimesLoaded            = 0;
                    newLink.CreatedByApplicationId = app.Id;
                    RedirectItem ri = _DAL.AddNewRedirectItem(newLink);

                    if (ri != null)
                    {
                        logger.Info("API POST /link - successfully created", ri);
                        return(StatusCode(201, new ApiPostResponse(ri)));
                    }
                    else
                    {
                        var e = new SimpleError("An error has occured, please try again");
                        logger.Error("API POST /link", e);
                        return(StatusCode(500, e));
                    }
                }
                else
                {
                    var e = new SimpleError("Invalid API Key");
                    logger.Error("API POST /link", e);
                    return(BadRequest(e));
                }
            }
            catch (Exception e)
            {
                if (e.GetType() == typeof(FormatException) && e.Message.Contains("is not a valid 24 digit hex string") ||
                    e.GetType() == typeof(InvalidOperationException) && e.Message.Contains("Sequence contains no elements"))
                {
                    logger.Error("API POST /link - Invalid API Key " + apiKey, e);
                    return(BadRequest(new SimpleError("Invalid API Key")));
                }

                logger.Error("Unknown error occured", e);
                return(StatusCode(500, new SimpleError("An error has occured, please try again")));
            }
        }
        /// <summary>
        /// Sets the properties from an exception.
        /// </summary>
        /// <param name="exception">The exception to populate properties from.</param>
        /// <param name="client">
        /// The ExceptionlessClient instance used for configuration. If a client is not specified, it will use
        /// ExceptionlessClient.Default.
        /// </param>
        public static SimpleError ToSimpleErrorModel(this Exception exception, ExceptionlessClient client = null)
        {
            if (client == null)
            {
                client = ExceptionlessClient.Default;
            }

            var  log  = client.Configuration.Resolver.GetLog();
            Type type = exception.GetType();

            var error = new SimpleError {
                Message    = GetMessage(exception),
                Type       = type.FullName,
                StackTrace = exception.StackTrace
            };

            var exclusions = _exceptionExclusions.Union(client.Configuration.DataExclusions).ToList();

            try {
                if (exception.Data != null)
                {
                    foreach (object k in exception.Data.Keys)
                    {
                        string key = k != null?k.ToString() : null;

                        if (String.IsNullOrEmpty(key) || key.AnyWildcardMatches(exclusions, true))
                        {
                            continue;
                        }

                        error.Data[key] = exception.Data[k];
                    }
                }
            } catch (Exception ex) {
                log.Error(typeof(ExceptionlessClient), ex, "Error populating Data: " + ex.Message);
            }

            try {
                var extraProperties = type.GetPublicProperties().Where(p => !p.Name.AnyWildcardMatches(exclusions, true)).ToDictionary(p => p.Name, p => {
                    try {
                        return(p.GetValue(exception, null));
                    } catch {}
                    return(null);
                });

                extraProperties = extraProperties.Where(kvp => !ValueIsEmpty(kvp.Value)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                if (extraProperties.Count > 0 && !error.Data.ContainsKey(SimpleError.KnownDataKeys.ExtraProperties))
                {
                    error.AddObject(new ExtendedDataInfo {
                        Data = extraProperties,
                        Name = SimpleError.KnownDataKeys.ExtraProperties,
                        IgnoreSerializationErrors = true,
                        MaxDepthToSerialize       = 5
                    }, client);
                }
            } catch {}

            if (exception.InnerException != null)
            {
                error.Inner = exception.InnerException.ToSimpleErrorModel(client);
            }

            return(error);
        }
Exemple #8
0
 protected bool Equals(SimpleError other)
 {
     return(String.Equals(Message, other.Message) && String.Equals(Type, other.Type) && String.Equals(StackTrace, other.StackTrace) && Equals(Data, other.Data) && Equals(Inner, other.Inner));
 }