Пример #1
0
        private static bool CheckPropertiesUniqueness(this ISchema schema, out Exception exception)
        {
            var fieldInfos    = schema.Properties;
            var distinctCount = fieldInfos.Select(x => x.Name).Distinct().Count();

            if (fieldInfos.Count != distinctCount)
            {
                if (schema is IFieldInfo fieldInfo)
                {
                    exception = new FieldValidationException("Duplicate property declaration. Property names are must be unique.", fieldInfo);
                }
                else
                {
                    exception = new SchemaValidationException("Duplicate property declaration. Property names are must be unique.");
                }

                return(false);
            }

            foreach (var fieldInfo in fieldInfos)
            {
                if (fieldInfo is ISchema subObjectSchema)
                {
                    var isValid = CheckPropertiesUniqueness(subObjectSchema, out exception);
                    if (!isValid)
                    {
                        return(false);
                    }
                }
            }

            exception = null;
            return(true);
        }
Пример #2
0
      public object PersonPost(Person doc)
      {
        var puzzlePass = false;
        WorkContext.NeedsSession();
        if (WorkContext.Session != null && doc.Puzzle != null)
        {
          var pk = WorkContext.Session["PersonPuzzle"] as PuzzleKeypad;
          if (pk != null)
          {
            var answer = doc.Puzzle["Answer"] as JsonDataArray;
            if (answer != null)
              puzzlePass = pk.DecipherCoordinates(answer) == pk.Code;
          }
        }

        Exception error = null;
        if (puzzlePass)
        {
          doc.YearsInService++;
          error = doc.Validate();
        }
        else
          error = new FieldValidationException("Person", "Puzzle", "Please answer the question correctly");

        if (doc.Puzzle != null)
        doc.Puzzle.Remove("Answer");

        makePuzzle();
        return new ClientRecord(doc, error);
      }
        public static void ConfigureGlobalExceptionHandler(this IApplicationBuilder app)
        {
            app.UseExceptionHandler(appError =>
            {
                appError.Run(async context =>
                {
                    var contextFeature = context.Features.Get <IExceptionHandlerFeature>();
                    if (contextFeature?.Error != null)
                    {
                        context.Response.ContentType = "application/json";

                        object errorModel;
                        switch (contextFeature.Error)
                        {
                        case ValidationException validationException:
                            context.Response.StatusCode = (int)validationException.StatusCode;
                            errorModel = new ErrorModel <IEnumerable <string> >
                            {
                                Message    = contextFeature.Error.Message,
                                ErrorCode  = validationException.ErrorCode,
                                StatusCode = (int)validationException.StatusCode,
                                Data       = validationException.Errors
                            };
                            break;

                        case CumulativeValidationException cumulativeValidationException:
                            context.Response.StatusCode = 400;
                            errorModel = new
                            {
                                contextFeature.Error.Message,
                                ErrorCode  = "ValidationException",
                                StatusCode = 400,
                                Errors     = cumulativeValidationException.Errors.Select(x => new
                                {
                                    x.Message,
                                    x.FieldName,
                                    x.FieldPath
                                })
                            };
                            break;

                        case ErtisException ertisException:
                            context.Response.StatusCode = (int)ertisException.StatusCode;
                            errorModel = new ErrorModel
                            {
                                Message    = contextFeature.Error.Message,
                                ErrorCode  = ertisException.ErrorCode,
                                StatusCode = (int)ertisException.StatusCode
                            };
                            break;

                        case HttpStatusCodeException httpStatusCodeException:
                            context.Response.StatusCode = (int)httpStatusCodeException.StatusCode;
                            errorModel = new ErrorModel
                            {
                                Message    = contextFeature.Error.Message,
                                ErrorCode  = httpStatusCodeException.HelpLink,
                                StatusCode = (int)httpStatusCodeException.StatusCode
                            };
                            break;

                        case ErtisSchemaValidationException ertisSchemaValidationException:
                            context.Response.StatusCode = 400;
                            errorModel = contextFeature.Error switch
                            {
                                FieldValidationException fieldValidationException => new
                                {
                                    fieldValidationException.Message,
                                    fieldValidationException.FieldName,
                                    fieldValidationException.FieldPath,
                                    ErrorCode  = "FieldValidationException",
                                    StatusCode = 400
                                },
                                SchemaValidationException schemaValidationException => new
                                {
                                    schemaValidationException.Message,
                                    ErrorCode  = "SchemaValidationException",
                                    StatusCode = 400
                                },
                                _ => new ErrorModel
                                {
                                    Message    = ertisSchemaValidationException.Message,
                                    ErrorCode  = "SchemaValidationException",
                                    StatusCode = 400
                                }
                            };
                            break;

                        default:
                            context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                            errorModel = new ErrorModel
                            {
                                Message    = contextFeature.Error.Message,
                                ErrorCode  = "UnhandledExceptionError",
                                StatusCode = 500
                            };

                            break;
                        }

                        var json = Newtonsoft.Json.JsonConvert.SerializeObject(errorModel);
                        await context.Response.WriteAsync(json);
                        await context.Response.CompleteAsync();
                    }
                });
            });
        }