HttpEntity and responce to a try catch - http

i am learning how to manage some http request with Spring Framework and create same ape to read/write on a Database.
So far i have this methods:
#GetMapping("/{id}")
#Override
public Book getById(#PathVariable("id") int id) {
logger.debug("get book with id: " + id);
try {
return bookService.getById(id);
} catch (BookNotPresentException e) {
logger.debug("book not present" + e);
// TODO ritornare un http entity
// -> meglio fare handler dedicato!
return new Book(-1, "Book not Present", null, null, 0, 0, null, 0);
}
}
my big problem is how to manage the catch phase.
What i have to do? the idea is return somehow a string with "book not present".
Can you suggest something ?
thanks :)

I did find a solution here: https://www.baeldung.com/exception-handling-for-rest-with-spring
and use the 5th solution proposed.

Related

is doing a commit at the end of a line of code a good thing?

I have code like this, where the commit is done at the end of the line. the goal is that when an error occurs while sending an email, then the commit will not be performed but will rollback. is something like this a good thing? or is there a better way than the one I'm currently implementing?
public async Task<IActionResult> Register(RegisterAccount register)
{
MyAccount myAccount = new();
using var transaction = _dbContext.Database.BeginTransaction();
bool afterRegister = false;
try
{
//code for check account
//code for set value register account
afterRegister = true;
_dbContext.Database.OpenConnection();
transaction.CreateSavepoint("register");
_dbContext.MyAccounts.Add(myAccount);
await _dbContext.SaveChangesAsync();
_dbContext.Database.CloseConnection();
//code for send email
transaction.Commit();
return RedirectToAction("RegisterConfirm", "Account", new { emailConfirm = myAccount.Email });
}
catch(Exception e)
{
if (afterRegister)
{
transaction.RollbackToSavepoint("register");
}
return View();
}
}
thank you for answering my question. good luck always

Unable to Access SQLite Data in MvvmCross ViewModel

Hello StackOverflow community,
I know there's a lot of code in this post, but I wanted to give you guys, the community as good of a picture as possible as to what is going on here so that maybe someone can help me figure out what my issue is.
Recently for a project I'm working on we've decided to upgrade from MvvmCross 5.7.0 to 6.2.2. I've managed to get our UWP app to successfully complete the initialization and setup process. The first viewmodel for which we register the app start also starts initializing. However, I'm finding that my vm initialization hangs at a particular line of code (shown in the code below). The weirdest part though is similar methods called in the app initialization code run perfectly fine without hanging/deadlock, so I'm not sure what's different Here's a simplified version of my viewmodel code to illustrate:
public class MyViewModel : BaseAuthenticatedTabBarViewModel, IMvxViewModel<int>
{
private int? _settingValue;
public override async Task Initialize()
{
//Some irrelevant initialization code
Exception e = null;
try
{
//This line of code never returns
_settingValue = _settingValue ?? await AppSettingService.GetSettingValue();
}
catch (Exception ex)
{
e = ex;
}
if (e != null)
{
await HandleCatastrophicError(e);
}
}
}
The AppSettingService.GetSettingValue() method looks like this:
public async Task<int?> GetCurrentEventId()
{
return await GetNullableIntSetting("SettingValue");
}
private static async Task<int?> GetNullableIntSetting(string key)
{
try
{
var setting = await SettingDataService.SettingByName(key);
if (setting != null)
{
return string.IsNullOrEmpty(setting.Value) ? (int?)null : Convert.ToInt32(setting.Value);
}
}
catch (Exception ex)
{
//Handle the exception
}
return null;
}
All the code for SettingDataService:
public class SettingDataService : DataService<SettingDataModel>, ISettingDataService
{
public async Task<SettingDataModel> SettingByName(string name)
{
try
{
var values = (await WhereAsync(e => e.Name == name));
return values.FirstOrDefault();
}
catch(Exception ex)
{
//Handle the exception
}
return null;
}
}
Finally, the implementation for WhereAsync() is in a class called DataService and is as follows:
public virtual async Task<IEnumerable<T>> WhereAsync(System.Linq.Expressions.Expression<Func<T, bool>> condition, SQLiteAsyncConnection connection = null)
{
return await (connection ?? await GetConnectionAsync())
.Table<T>()
.Where(condition)
.ToListAsync();
}
Thank you very much for your help in advance
Edit: Forgot to also add this crucial bit of code to help you guys even further:
protected async Task<SQLiteAsyncConnection> GetConnectionAsync()
{
SQLiteAsyncConnection connection = null;
while (true)
{
try
{
connection = Factory.Create(App.DatabaseName);
// This line of code is the culprit. For some reason this hangs and I can't figure out why.
await connection.CreateTableAsync<T>();
break;
}
catch (SQLiteException ex)
{
if (ex.Result != Result.CannotOpen && ex.Result != Result.Busy && ex.Result != Result.Locked)
{
throw;
}
}
await Task.Delay(20);
}
return connection;
}
I'm suspecting that you are calling Task.Wait or Task<T>.Result somewhere further up your call stack. Or if you're not doing it, MvvmCross is probably doing it for you. This will cause a deadlock when called from a UI context.
Personally, I prefer the approach that ViewModels should always be constructed synchronously, and cannot have an asynchronous "initialization". That is, they must construct themselves (synchronously) into a "loading" state, and this construction can kick off an asynchronous operation that will later update them into a "loaded" state. The synchronous-initialization pattern means there's never an unnecessary delay when changing views; your users may only see a spinner or a loading message, but at least they'll see something. See my article on async MVVM data binding for a pattern that helps with this, and note that there's a newer version of the helper types in that article.

Can't return completedsuccesfull task in .net core

Hi I wrote the following code:
private bool GetIsCompleted()
{
return Email.SendMessageAsync().IsCompletedSuccessfully;
}
[HttpPost]
public ViewResult CheckOut(Order order)
{
if (Cart.Lines.Count() == 0)
{
ModelState.AddModelError("","Your Cart is empty!");
}
if (ModelState.IsValid)
{
order.CartLines = Cart.Lines;
order.DateTime = DateTime.Now;
order.TotalPrice = Cart.ComputeTotalValue();
if (Repository.SaveOrder(order))
{
if (User.Identity.Name != null)
{
Email.SetMessageBody(order.OrderID);
if (GetIsCompleted())
{
Cart.Clear();
return View("Completed");
}
}
}
ViewBag.Error = "An error Occured while sending you an email with the order details.";
return View(new Order());
}
else
{
ViewBag.Error = "An error Occured while trying to save your order. Please try again!";
return View(new Order());
}
}
public async Task SendMessageAsync()
{
this.Message = new MailMessage(this.MailFrom.ToString(), this.MailTo.ToString(), this.GetSubject(), this.GetMessageBody());
//Message.Dispose();
try
{
await this.Client.SendMailAsync(this.Message);
}
catch (Exception ex)
{
Logger.LogInformation("The Email couldn't send to the recipient");
}
}
I get
An error Occured while sending you an email with the order details.
in the View. I want GetIsCompleted() to return true to proceed the code. It is developed under .net core. I do not understand why IsCompletedSuccessfully() does not return true; Any suggestion?
The current flow of your code is this:
Start sending the e-mail.
Check if it is completed successfully, decide that it hasn't and return failure.
The e-mail completes sending.
You're awaiting the actual SendMailAsync(..) method, and that's great, but nothing awaits SendMessageAsync(...) so it immediately returns the incomplete task to the caller. Because there isn't enough time between starting to send the e-mail and checking if the task completed, the status will be false.
You need to use async all the way up. Change your method definition to be async:
public async Task<ViewResult> CheckOut(Order order)
Replace this code:
if (GetIsCompleted())
{
Cart.Clear();
return View("Completed");
}
with this:
try
{
await Email.SendMessageAsync();
Cart.Clear();
return View("Completed");
}
catch (Exception e)
{
// handle exception
}
It's worth noting that you'll only ever get an exception if the call to new MailMessage(...) fails because your try/catch block in SendMessageAsync is swallowing all other exceptions.

ASP.NET MVC Exception Handling with AJAX/JSON

I have several methods in a controller that look like:
[HttpPost]
public ActionResult AddEditCommentToInvoice(string invoiceNumber, string comments)
{
var response = new { success = true, msg = "Comment saved", statusMsg = "Comment saved" };
try
{
var recordsModified = invoiceService.AddCommentsToInvoice(invoiceNumber, comments);
Log.Info(recordsModified ? "Updated Comment" : "Did not update Comment");
} catch (Exception ex) {
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return Json(new {
success = false,
msg = "There is missing field data",
statusMsg = ex.Message
}, JsonRequestBehavior.AllowGet);
}
return Json(response, JsonRequestBehavior.AllowGet);
}
While this code works, I'm not comfortable with this approach because:
Try/Catches are expensive
The code catches System.Exception
The code is ugly
Now I know that I can use OnException or the HandleError attribute.
I also did some research on ELMAH and this looks promising.
But I still want to return JSON via AJAX to my user to indicate whether the operation was a success or not.
So my question is, has anyone used any of the three methods (or specifically ELMAH) to return JSON via AJAX?
I use another approach that's an approach that can be applied at the controller level or globally through GlobalFilters. In my MVC controllers, you could override OnActionExecuted method, and do this:
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.Exception != null)
{
filterContext.Result = Json(new { success = false });
return;
}
base.OnActionExecuted(filterContext);
}
This could also be done as an action filter attribute. You wouldn't need any exception handling in your controllers - if an exception occurs, then this is handled within the context of the result.

JsonObjectRequest between Web Api and Android application

I have tried mostly everything, I'm so frustrated at this stage, I would love if I can get an json object on the android side to use to populate a viewlist, I have tried formatting the json being sent in every way I possibly know how. Any suggestions would be amazing and very appreciated.
The JsonStringRequest recieves the json fine as a string, however to parse it to something useful has been a pain and I haven't managed to get a efficient way for the whole project. Trying to convert the string to either an JsonObject or Array fail with the same error.
The JsonArray- and Object-Request have both failed with similar error.
I have a feeling its not android side but the way I am sending the json from the rest service, or the way Volley is handling it.
Asp.net Web Api Controller class
using Newtonsoft.Json;
[RoutePrefix("users")]
public class UsersController : ApiController
{
public IHttpActionResult getUsers()
{
User[] users = new User[]
{
new User { userID = "one", Name = "Tomato Soup", Surname = "Groceries", Nickname = "boogie" },
new User { userID = "two", Name = "Tomato Soup", Surname = "Groceries", Nickname = "boogie" }
};
string json = JsonConvert.SerializeObject(users);
return Json(json);
}
}
The JSON for what is being sent:
"[{\"userID\":\"one\",\"Name\":\"Tomato Soup\",\"Surname\":\"Groceries\",\"Nickname\":\"boogie\"},{\"userID\":\"two\",\"Name\":\"Tomato Soup\",\"Surname\":\"Groceries\",\"Nickname\":\"boogie\"}]"
The XML for what is being sent:
[{"userID":"one","Name":"Tomato Soup","Surname":"Groceries","Nickname":"boogie"},{"userID":"two","Name":"Tomato Soup","Surname":"Groceries","Nickname":"boogie"}]
Android side request:
JsonObjectRequest (Very similar to JsonArrayRequest):
JsonObjectRequest request = new JsonObjectRequest("http://10.0.0.3/users",
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response)
{
Log.w("##########", response.toString());
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.w("##########", error.getMessage());
}
}
) ;
queue.add(request);
StringRequest:
StringRequest req = new StringRequest("http://10.0.0.3/users", new Response.Listener<String>(){
public void onResponse(String response)
{
try
{
JSONObject jar = new JSONObject(response);
}
catch(JSONException error)
{
Log.w("error", error.getMessage());
}
Log.w("############", response);
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error)
{
Log.w("error", error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG);
}
});
queue.add(req);
Logcat error:
org.json.JSONException: Value [{"userID":"one","Name":"Tomato Soup","Surname":"Groceries","Nickname":"boogie"},{"userID":"two","Name":"Tomato Soup","Surname":"Groceries","Nickname":"boogie"}] of type java.lang.String cannot be converted to JSONObject
Because the result is a JSONArray, you can use a JsonArrayRequest instead of JsonObjectRequest. If you have used JsonArrayRequest already and had any error, please post the error message or any logcat information with that JsonArrayRequest.
You can go here and here for more information
JSONObject: ...A string beginning with { (left brace) and ending with } (right brace).
JSONArray: ...A string that begins with [ (left bracket) and ends with ] (right bracket).
Hope this helps!
Wow, I figured it out. I was double serializing the user array object, it seems if I return the object without using the JsonConvert.SerializeObject it automatically serializes the object into a json. I'm not sure if its because its inherent to web api 2 (haven't read all the documentation yet). But thanks anyway.

Resources