private static Task HandleInputValidationErrors(HttpContext httpContext, InvalidInputException ex) { httpContext.Response.ContentType = "application/json"; //ToDo: replace it! httpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; return(httpContext.Response.WriteAsync(ex.GetBadRequestBody())); }
public static void AddInputError(this IRequestContext context, InvalidInputException exc) { var path = exc.Anchor.GetRequestObjectPath(); var loc = exc.Anchor.SourceLocation; var err = new GraphQLError(exc.Message, path, loc, ErrorCodes.InputError); context.AddError(err); }
public static ErrorResponse ToErrorResponse(this InvalidInputException src) { var validationErrors = src.Errors.SelectMany(error => error.MemberNames.Select(member => (Member: member, Message: error.ErrorMessage))); var errorResponse = ErrorResponse.Create(src.Message); foreach (var error in validationErrors) { errorResponse.AddModelError(error.Member, error.Message); } return(errorResponse); }
public void InvalidInput() { int Inc(int count, Toggle toggle) => count + 1; TypeDrivenStateMachine <State, int> machine = TypeDrivenStateMachine <State, int> .Builder() .Register <Toggle>(State.On, State.Off, Inc) .Register <Toggle>(State.Off, State.On, Inc) .Build(State.Off, 0); Assert.False(machine.IsValid("foo")); InvalidInputException exception = Assert.Throws <InvalidInputException>(() => machine.Apply("foo")); Assert.Equal("foo", exception.Input); Assert.Equal(State.Off, exception.State); Assert.Equal(new List <Type> { typeof(Toggle) }, exception.ValidInputTypes); Assert.Equal("Value \"foo\" of type \"System.String\" is not a valid input from state \"Off\". Valid input types: Toggle", exception.Message); }
public ErrorsProblemDetails(InvalidInputException exception) : base((int)HttpStatusCode.BadRequest) { Detail = exception.Message; Errors = exception.Errors; }
static void Main(string[] args) { //I use the separate class CoordinatesSetter to set width and height Console.WriteLine("Enter coordinates: "); var setters = new CoordinatesSetter(); int x; int y; try { x = setters.SetWidth(); y = setters.SetHeight(); if (x >= 1000 || y >= 1000) { var ex = new NumberOutOfRangeException("Number shouldn't be equal or more than 1000!"); Console.WriteLine(ex); return; } if (x > y) { var ex = new NumberOutOfRangeException("Width shouldn't be more than height!"); Console.WriteLine(ex); return; } } catch { var ex = new InvalidInputException("Invalid input for width or height. You should enter a number!"); Console.WriteLine(ex.Message); return; } //Class for creating a matrix var grid = new Grid(x, y); var matrix = grid.GenerationZero; // I use class GridActions, where are stored the methods for calculating, filling and etc var gridActions = new GridActions(); Console.WriteLine("Fill matrix"); gridActions.FillMatrix(matrix); //I set the coordinates for the selected item and the number of generations Console.WriteLine("Enter coordinates for the selected element"); int x1 = setters.SetWidthForElement(); int y1 = setters.SetHeightForElement(); if (x1 < 0 || x1 > x || y1 < 0 || y1 > y) { var ex = new ItemOutOfGridException("The item is outside of the grid!"); Console.WriteLine(ex); return; } Console.WriteLine("Enter N generations:"); int n = setters.SetGenerations(); var result = gridActions.Calculate(x1, y1, n, matrix); // In my opinion the answer of the first example should be 6, not 5. // I checked all of the conditions, debugged the code many times and drawed every state of the matrix to proof this probability. // The condition is that we have to do calculations in Generation Zero and N, so we have 11 generations at all. // At Generation Zero the selected item is GREEN (count = 1). // After that we have two states of the matrix, which switch themselfes every iteration (count = 5). So we have 6 times at all. Console.WriteLine($"The cell has been green in {result} calculations."); }
public InvalidInputExceptionHandler(InvalidInputException invalidInputException) { _invalidInputException = invalidInputException; }
public static string GetBadRequestBody(this InvalidInputException ex) { return(JsonConvert.SerializeObject( new Error((int)HttpStatusCode.BadRequest, ErrorCodes.InvalidInputError, ex.Message))); }