From 0ab412b6cf49d90d86fd4004d4a3de263f6e1ee3 Mon Sep 17 00:00:00 2001 From: Marco Audiffredi Date: Wed, 2 Apr 2025 15:48:42 +0200 Subject: [PATCH] gestione errore --- Controllers/AddDestinazioneController.cs | 10 +++---- Controllers/ConsegneController.cs | 36 ++---------------------- Controllers/DestinazioniController.cs | 18 ++++++------ Controllers/GiriChiudiController.cs | 10 +++---- Controllers/GiriController.cs | 30 ++++++++------------ Controllers/GiriDaValidareController.cs | 14 ++++++--- Controllers/GiriEliminaController.cs | 3 +- Controllers/LoginController.cs | 3 +- SessionHelper.cs | 6 ++++ 9 files changed, 52 insertions(+), 78 deletions(-) diff --git a/Controllers/AddDestinazioneController.cs b/Controllers/AddDestinazioneController.cs index 6c3744a..eda8800 100644 --- a/Controllers/AddDestinazioneController.cs +++ b/Controllers/AddDestinazioneController.cs @@ -31,7 +31,7 @@ namespace SoftwayWeb.Controllers public IActionResult AddDestinazione(string serialeGiro, string? codAutista, string? nomeAutista, string? codMezzo, string? desMezzo, DateTime dataGiro) { SessionHelper helper = new SessionHelper(this); - + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) @@ -69,7 +69,7 @@ namespace SoftwayWeb.Controllers public IActionResult NewDestinazioneStep1(Modgir modgir, string nomeAutista, string automezzo, DateTime dataGiro) { SessionHelper helper = new SessionHelper(this); - + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) @@ -112,7 +112,7 @@ namespace SoftwayWeb.Controllers } else { - errMes = response.Content.ReadAsStringAsync().Result; + errMes = helper.getErrMsgFromResponse(response); helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } @@ -127,7 +127,7 @@ namespace SoftwayWeb.Controllers public IActionResult NewDestinazione(Modgir modgir /*,string? codAutista, string? codMezzo, DateTime dataGiro, string serGiro*/) { SessionHelper helper = new SessionHelper(this); - + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) @@ -182,7 +182,7 @@ namespace SoftwayWeb.Controllers } else { - errMes = response.Content.ReadAsStringAsync().Result; + errMes = helper.getErrMsgFromResponse(response); helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } diff --git a/Controllers/ConsegneController.cs b/Controllers/ConsegneController.cs index 748f220..62da606 100644 --- a/Controllers/ConsegneController.cs +++ b/Controllers/ConsegneController.cs @@ -27,6 +27,7 @@ namespace SoftwayWeb.Controllers public IActionResult Dettaglio(string id, string? codAutista, DateTime dataGiro, string? codMezzo) { helper = new SessionHelper(this); + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); urlBase = apiUrl + "Giri/listaDestinazioniByAutistaDataMezzo"; var dataGiroFormattata = dataGiro.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'"); @@ -52,46 +53,13 @@ namespace SoftwayWeb.Controllers } else { - errMes = response.Content.ReadAsStringAsync().Result; + errMes = helper.getErrMsgFromResponse(response); helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } } - //public IActionResult Index(string id, string? codAutista, DateTime dataGiro, string? codMezzo) - //{ - // helper = new SessionHelper(this); - // token = helper.GetStringValue("tok"); - // urlBase = apiUrl + "Giri/listaDestinazioniByAutistaDataMezzo"; - // var dataGiroFormattata = dataGiro.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'"); - // urlBase = urlBase + "?autista=" + codAutista + "&dataGiro="+ dataGiroFormattata + "&mezzo=" + codMezzo; - // Uri baseAddress = new Uri(urlBase); - // client = new HttpClient(); - // client.BaseAddress = baseAddress; - // List modelList = new List(); - // Destinazioni_Out model = new Destinazioni_Out(); - // Destinazioni_Out.ConsegnaFatta modelCons = new Destinazioni_Out.ConsegnaFatta(); - - // HttpResponseMessage response = client.GetAsync(baseAddress).Result; - - // if (response.IsSuccessStatusCode) - // { - // string dato = response.Content.ReadAsStringAsync().Result; - // modelList = JsonConvert.DeserializeObject>(dato); - // model = modelList.Where(x => x.Brserial == id).First(); - // modelCons = model.ConsFatta; - - // return View(modelCons); - // } - // else - // { - // errMes = response.Content.ReadAsStringAsync().Result; - // helper.SetStringValue("errMsg", errMes); - // return RedirectToAction("Error"); - // } - - //} } } diff --git a/Controllers/DestinazioniController.cs b/Controllers/DestinazioniController.cs index f074dde..9c46f56 100644 --- a/Controllers/DestinazioniController.cs +++ b/Controllers/DestinazioniController.cs @@ -41,7 +41,7 @@ namespace SoftwayWeb.Controllers public IActionResult Index(string id, string? codAutista, string? nomeAutista, DateTime dataGiro, string? codMezzo, string? desMezzo, int? page = 1) { helper = new SessionHelper(this); - + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) @@ -139,7 +139,7 @@ namespace SoftwayWeb.Controllers public IActionResult IndexValidate(string id, string? codAutista, string? nomeAutista, DateTime dataGiro, string? codMezzo, string? desMezzo, int? page = 1) { helper = new SessionHelper(this); - + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) @@ -237,7 +237,7 @@ namespace SoftwayWeb.Controllers public IActionResult EliminaDestinazione(string serial) { SessionHelper helper = new SessionHelper(this); - + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) @@ -328,7 +328,7 @@ namespace SoftwayWeb.Controllers public IActionResult ModificaDestinazione(string serial) { SessionHelper helper = new SessionHelper(this); - + helper.ClearFormatedKey("errMsg"); var requestUrl = $"{Request.Scheme}://{Request.Host.Value}/"; @@ -395,7 +395,7 @@ namespace SoftwayWeb.Controllers public IActionResult ModificaDestinazioneStep1(Destinazioni_Out model) { SessionHelper helper = new SessionHelper(this); - + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) @@ -462,7 +462,7 @@ namespace SoftwayWeb.Controllers } else { - errMes = response.Content.ReadAsStringAsync().Result; + errMes = helper.getErrMsgFromResponse(response); helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } @@ -475,7 +475,7 @@ namespace SoftwayWeb.Controllers public IActionResult ModificaDestinazionePost(Destinazioni_Out model) { SessionHelper helper = new SessionHelper(this); - + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) @@ -530,7 +530,7 @@ namespace SoftwayWeb.Controllers } else { - errMes = response.Content.ReadAsStringAsync().Result; + errMes = helper.getErrMsgFromResponse(response); helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } @@ -560,7 +560,7 @@ namespace SoftwayWeb.Controllers } else { - errMes = response2.Content.ReadAsStringAsync().Result; + errMes = helper.getErrMsgFromResponse(response2); helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } diff --git a/Controllers/GiriChiudiController.cs b/Controllers/GiriChiudiController.cs index 7a77040..4d32d31 100644 --- a/Controllers/GiriChiudiController.cs +++ b/Controllers/GiriChiudiController.cs @@ -27,7 +27,7 @@ namespace SoftwayWeb.Controllers public ActionResult Index() { SessionHelper helper = new SessionHelper(this); - + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) @@ -40,7 +40,7 @@ namespace SoftwayWeb.Controllers public ActionResult Chiudi(string id, int nbanc,decimal imp) { SessionHelper helper = new SessionHelper(this); - + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) @@ -71,7 +71,7 @@ namespace SoftwayWeb.Controllers } else { - errMes = response.Content.ReadAsStringAsync().Result; + errMes = helper.getErrMsgFromResponse(response); helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } @@ -84,7 +84,7 @@ namespace SoftwayWeb.Controllers { //https://api.poloinformatico.it:8000/api/Giri/closeGiro?serialeGiro=0000000002&bancaliRecuperati=11&importoRecuperato=100 SessionHelper helper = new SessionHelper(this); - + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) @@ -112,7 +112,7 @@ namespace SoftwayWeb.Controllers } else { - errMes = response.Content.ReadAsStringAsync().Result; + errMes = helper.getErrMsgFromResponse(response); helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } diff --git a/Controllers/GiriController.cs b/Controllers/GiriController.cs index 51263bb..6b42084 100644 --- a/Controllers/GiriController.cs +++ b/Controllers/GiriController.cs @@ -110,7 +110,7 @@ namespace SoftwayWeb.Controllers } else { - errMes = response.Content.ReadAsStringAsync().Result; + errMes = helper.getErrMsgFromResponse(response); helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } @@ -187,7 +187,7 @@ namespace SoftwayWeb.Controllers } else { - errMes = response.Content.ReadAsStringAsync().Result; + errMes = helper.getErrMsgFromResponse(response); helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } @@ -225,7 +225,7 @@ namespace SoftwayWeb.Controllers } else { - errMes = response.Content.ReadAsStringAsync().Result; + errMes = helper.getErrMsgFromResponse(response); helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } @@ -241,6 +241,7 @@ namespace SoftwayWeb.Controllers return View(model); } SessionHelper helper = new SessionHelper(this); + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) @@ -265,7 +266,7 @@ namespace SoftwayWeb.Controllers } else { - errMes = response.Content.ReadAsStringAsync().Result; + errMes = helper.getErrMsgFromResponse(response); helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } @@ -275,7 +276,7 @@ namespace SoftwayWeb.Controllers public IActionResult ModificaTutteDestinazioni(string id, string? codAutista, string? nomeAutista, string? codMezzo, string? nomeMezzo, DateTime dataGiroCons/*, int? page = 1*/) { helper = new SessionHelper(this); - + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) @@ -321,7 +322,7 @@ namespace SoftwayWeb.Controllers } else { - errMes = response.Content.ReadAsStringAsync().Result; + errMes = helper.getErrMsgFromResponse(response); helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } @@ -331,7 +332,7 @@ namespace SoftwayWeb.Controllers public IActionResult ModificaTutteDestinazioniStep1(string SerialeGiro, string? CodAutista, string? NomeAutista, string? CodMezzo, string? NomeMezzo, DateTime DataGiroCons,string CodAutistaOld,string CodMezzoOld) { SessionHelper helper = new SessionHelper(this); - + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) @@ -406,7 +407,7 @@ namespace SoftwayWeb.Controllers } else { - errMes = response.Content.ReadAsStringAsync().Result; + errMes = helper.getErrMsgFromResponse(response); helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } @@ -424,7 +425,7 @@ namespace SoftwayWeb.Controllers public IActionResult ModificaTutteDestinazioniPost(List modelList, string? CodAutista, string? CodMezzo, DateTime DataGiroCons,string SerialeGiro,string TipoOperazione,string SerialeGiroDestinazione) { SessionHelper helper = new SessionHelper(this); - + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) @@ -469,21 +470,12 @@ namespace SoftwayWeb.Controllers if (!response2.IsSuccessStatusCode) { - errMes = response2.Content.ReadAsStringAsync().Result; + errMes = helper.getErrMsgFromResponse(response2); helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } } - //return RedirectToAction("IndexValidate", "Destinazioni", new - //{ - // id = model.serialeGiro, - // codAutista = model.CodAutistaOld, - // nomeAutista = model.AutistaOld, - // dataGiro = model.DataCarico, - // codMezzo = model.CodAutomezzoOld, - // desMezzo = model.DescAutomezzoOld - //}); return RedirectToAction("Index", "Giri"); } diff --git a/Controllers/GiriDaValidareController.cs b/Controllers/GiriDaValidareController.cs index ec46905..85691c0 100644 --- a/Controllers/GiriDaValidareController.cs +++ b/Controllers/GiriDaValidareController.cs @@ -28,7 +28,8 @@ namespace SoftwayWeb.Controllers //se la data non รจ valorizzata, prendo i giri inseriti per ultimi, altrimenti li cerco per data List modelList = new List(); SessionHelper helper = new SessionHelper(this); - if(clear) + helper.ClearFormatedKey("errMsg"); + if (clear) { helper.SetStringValue("filterData", string.Empty); } @@ -54,7 +55,12 @@ namespace SoftwayWeb.Controllers string result = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(result); } - + else + { + errMes = helper.getErrMsgFromResponse(response); + helper.SetStringValue("errMsg", errMes); + return RedirectToAction("Error"); + } string _filtro= helper.GetStringValue("filterData"); if (data == DateTime.MinValue && string.IsNullOrEmpty(_filtro) && modelList.Count()>0) @@ -88,7 +94,7 @@ namespace SoftwayWeb.Controllers public IActionResult PostIndex(IList lst, string valida, string valida0) { SessionHelper helper = new SessionHelper(this); - + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) @@ -127,7 +133,7 @@ namespace SoftwayWeb.Controllers } else { - errMes = response.Content.ReadAsStringAsync().Result; + errMes = helper.getErrMsgFromResponse(response); helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } diff --git a/Controllers/GiriEliminaController.cs b/Controllers/GiriEliminaController.cs index 16dc9c9..9b8a17c 100644 --- a/Controllers/GiriEliminaController.cs +++ b/Controllers/GiriEliminaController.cs @@ -34,6 +34,7 @@ namespace SoftwayWeb.Controllers public IActionResult Elimina(string id) { SessionHelper helper = new SessionHelper(this); + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) @@ -74,7 +75,7 @@ namespace SoftwayWeb.Controllers public IActionResult Elimina(GiriConsegnaView model) { SessionHelper helper = new SessionHelper(this); - + helper.ClearFormatedKey("errMsg"); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) diff --git a/Controllers/LoginController.cs b/Controllers/LoginController.cs index 01a4c75..7a318fd 100644 --- a/Controllers/LoginController.cs +++ b/Controllers/LoginController.cs @@ -38,6 +38,7 @@ namespace SoftwayWeb.Controllers if (ModelState.IsValid) { SessionHelper helper = new SessionHelper(this); + helper.ClearFormatedKey("errMsg"); string url = apiUrl + "Login/loginMagazzino"; Uri baseAddress = new Uri(url); client.BaseAddress = baseAddress; @@ -90,7 +91,7 @@ namespace SoftwayWeb.Controllers { helper = new SessionHelper(this); helper.ClearFormatedKey("tok"); - + helper.ClearFormatedKey("errMsg"); return RedirectToAction("Login","Login"); } diff --git a/SessionHelper.cs b/SessionHelper.cs index 1b33b49..d351a68 100644 --- a/SessionHelper.cs +++ b/SessionHelper.cs @@ -60,5 +60,11 @@ namespace SoftwayWeb { _controller.HttpContext.Session.Remove(formatedKey); } + public string getErrMsgFromResponse(HttpResponseMessage response) + { + string msg=string.Empty; + msg = response.ToString(); + return msg; + } } }