/// <summary> /// Called after an action has thrown an <see cref="T:System.Exception" />. /// </summary> /// <param name="context">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.ExceptionContext" />.</param> public void OnException(ExceptionContext context) { if (context.Exception.GetType() == typeof(BLException)) { var exception = (BLException)context.Exception; var apiResponse = new WebApiResponseDTO <string>() { HttpStatusCode = HttpStatusCode.BadRequest, IsSuccess = false, Message = exception.Message, ObjResult = exception?.InnerException?.Message }; context.Result = new BadRequestObjectResult(apiResponse); context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; context.ExceptionHandled = true; } else { var exception = context.Exception; var apiResponse = new WebApiResponseDTO <string>() { HttpStatusCode = HttpStatusCode.InternalServerError, IsSuccess = false, Message = exception.Message, ObjResult = exception?.InnerException?.Message }; context.Result = new BadRequestObjectResult(apiResponse); context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; context.ExceptionHandled = false; } }
/// <summary> /// Configures the services. This method gets called by the runtime. Use this method to add services to the container. /// </summary> /// <param name="services">The services.</param> public void ConfigureServices(IServiceCollection services) { services.AddControllers(options => options.Filters.Add <GlobalExceptionFilter>()) .AddJsonOptions(options => options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter())) .AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore); ////Set data access connections services.AddDbContext <DigitalWareDavinCiInvoiceSystemContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DaVinciInvoiceSystem"))); ////Repositories services.AddScoped(typeof(IBaseRepo <>), typeof(BaseRepo <>)); ////BL Services services.AddScoped <IProductServiceBL, ProductServiceBL>(); services.AddScoped <IInvoiceServiceBL, InvoiceServiceBL>(); services.AddScoped <IRandomDataService, RandomDataService>(); services.AddScoped <ICustomerServiceBL, CustomerServiceBL>(); ////Model State Result (For API Controller) services.Configure <ApiBehaviorOptions>(options => { options.InvalidModelStateResponseFactory = context => { var apiReponse = new WebApiResponseDTO <IEnumerable <string> >() { HttpStatusCode = HttpStatusCode.BadRequest, IsSuccess = false, Message = "Verifique los datos ingresados", ObjResult = context.ModelState.Values.SelectMany(v => v.Errors)?.Select(x => x.ErrorMessage) }; return(new BadRequestObjectResult(apiReponse)); }; }); ////Configure swagger doc services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "DigitalWare DaVinci Diego Apply Test", Version = "v1", Description = "DaVinci Invoice System" }); c.AddEnumsWithValuesFixFilters(); }); }
public HttpResponseMessage Save([FromBody] AddOperateRecordRequest request) { var response = new WebApiResponseDTO <string>(); try { VerifyAuth(request); if (request.Sn.IsNullOrBlank()) { throw new Exception("Sn is empty"); } if (request.OperateTime.IsNullOrBlank()) { throw new Exception("OperateTime is empty"); } var result = _service.Add(new OperateRecordDTO() { Sn = request.Sn, Operation = request.Operation, Message = request.Message, OperatorName = request.OperatorName, OperateTime = DateTime.Parse(request.OperateTime), UserId = request.UserId.IsNullOrBlank() ? (Guid?)null : Guid.Parse(request.UserId), Ip = request.Ip, }); response.Succeeded = true; response.Message = "ok"; } catch (Exception ex) { Logger.Error(ex); response.Message = ex.Message; } return(GetJsonHttpResponseMessage(response)); }
public HttpResponseMessage Query([FromBody] QueryOperateRecordRequest request) { var response = new WebApiResponseDTO <List <OperateRecordDTO> >(); try { VerifyAuth(request); var limitCount = 50; if (request.LimitCount.NotNullOrBlank() && request.LimitCount != "0") { if (!ValidatePositiveInteger(request.LimitCount)) { throw new Exception("LimitCount invalid: " + request.Timestamp); } limitCount = Convert.ToInt32(request.LimitCount); if (limitCount < 0 || limitCount > 200) { throw new Exception("LimitCount must between 1~200"); } } var result = _service.FindBy(request.Sn, 1, limitCount); response.Succeeded = true; response.Message = "ok"; response.Data = result.ToList(); } catch (Exception ex) { Logger.Error(ex); response.Message = ex.Message; } return(GetJsonHttpResponseMessage(response)); }