public void AddAttendanceNoPerson() { int groupId = 111; int locationId = 14; int scheduleId = 19; DateTime occurrenceDate = new DateTime(2019, 8, 5); int? personId = null; int? personAliasId = null; var attendancesController = new AttendancesController(); Rock.Model.Attendance attendance = new Rock.Model.Attendance(); System.Web.Http.HttpResponseException exception = null; try { attendance = attendancesController.AddAttendance(groupId, locationId, scheduleId, occurrenceDate, personId, personAliasId); } catch (System.Web.Http.HttpResponseException ex) { exception = ex; } finally { Assert.IsTrue(exception.Response.StatusCode == System.Net.HttpStatusCode.BadRequest); } }
public override async Task <IList <T> > Post(IList <T> postedObjs) { var materializer = this.MaterializerFactory <EntityFrameworkMaterializer>(); List <T> materialList = new List <T>(); foreach (T postedObj in postedObjs) { DbContext context = materializer.DbContext; var material = await materializer.MaterializeUpdateAsync(postedObj); if (context.Entry <T>(material).State == EntityState.Added) { await context.SaveChangesAsync(); materialList.Add(material); } else { // POST should only create an object--if the EntityState is Unchanged or Modified, this is an illegal operation. var e = new System.Web.Http.HttpResponseException(System.Net.HttpStatusCode.BadRequest); //e.InnerException = new ArgumentException("The POSTed object already exists!"); // Can't do this, I guess... throw e; } } return(materialList); }
public override void OnException(HttpActionExecutedContext context) { ErrorMessageObject errorMessage = new ErrorMessageObject(); if (context.Exception is ArgumentNullException) { ArgumentNullException e = context.Exception as ArgumentNullException; errorMessage.Message = NULL_PARAMETERS; errorMessage.InternalMessage = string.Format("{0}. Parametro: {1}", NULL_RESPONSE, e.ParamName); errorMessage.ErrorCode = (int)HttpStatusCode.BadRequest; context.Response = new HttpResponseMessage(HttpStatusCode.BadRequest); } else if (context.Exception is System.Web.Http.HttpResponseException) { System.Web.Http.HttpResponseException webException = context.Exception as System.Web.Http.HttpResponseException; //string t = await (webException.Response.Content.ReadAsStringAsync()); errorMessage.Message = "Problemas con el request"; errorMessage.InternalMessage = webException.Response.ReasonPhrase; context.Response = new HttpResponseMessage(webException.Response.StatusCode); errorMessage.ErrorCode = (int)webException.Response.StatusCode; //errorMessage.InternalMessage = await webException.Response.Content.ReadAsStringAsync() ; } else { errorMessage.Message = "Error en la solicitud. Si el problema persiste, comuniquese con el administador."; errorMessage.InternalMessage = string.Format("Excepción no controlada de tipo {0}. Mensaje: {1}", context.Exception.GetType().ToString(), context.Exception.Message); } if (errorMessage.ErrorCode == 0) { context.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError); errorMessage.ErrorCode = (int)HttpStatusCode.InternalServerError; } context.Response.Content = new ObjectContent <ErrorMessageObject>(errorMessage, new JsonMediaTypeFormatter(), "application/json"); base.OnException(context); }