How to get error or success result from Acumatica Web service api? - api

//LoginResult loginResult = context.Login("user","pass");
//if (loginResult.Code != ErrorCode.OK)
//Get Schema
//Insert
//Add fields values
//....
O301000.Actions.CopyOrder,
O301000.Actions.Save,
O301000.OrderSummary.OrderNbr
Submitresult = O301000.context.Submit(cmds);
How do I know if there was an error when inserting/saving the Order (or any other file)?
I just can find a value 'Submitresult.ErrorCode' like in the Login Result.
Mean while a have solve the issue, when inserting, by looking for the 'O301000.OrderSummary.OrderNbr' not null value.
But that does not works when updating a record.

You should always use a
try{Submitresult = O301000.context.Submit(cmds);}
catch(Exception ex){Console.WriteLine(ex.Message);}
when making these calls. If the SOAP calls returns an error, than the message is passed to the Exception object.

Related

How to I get the detail (custom error message) returned with a bad request status code? So that I can do an ASSERT on it

Hi so I am setting up some Integration tests (using Xunit) and I would like to run an Assert to check whether the correct custom error message is returned.
This is the data I need to get is in the following response see image...
detail: "Username must be unique" Don't worry this message will be modified to be more useful later on I am just wanting to get it working first
Required Info
This is the current code...
//Act
response = await _httpClient.PostAsync("CompleteUserSetup", formContent);
//Assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode) ; //Bad request should be returned
//TODO: check custom error message is correct
So hoping for...
ASSERT.Equal("Username must be unique", some code to get detail from response)
Okay so I figured out how to get the data I needed. I just needed to convert the result into an object and then I was able to pull the detail data that I needed.
var resultModel = await System.Text.Json.JsonSerializer.DeserializeAsync<Result>(response.Content.ReadAsStream(), JsonSerializerHelper.DefaultDeserialisationOptions);
var errorMessage = resultModel.detail;

How do I get the message from an API using Flurl?

I've created an API in .NET Core 2 using C#. It returns an ActionResult with a status code and string message. In another application, I call the API using Flurl. I can get the status code number, but I can't find a way to get the message. How do I get the message or what do I need to change in the API to put the message someway Flurl can get it?
Here's the code for the API. The "message" in this example is "Sorry!".
[HttpPost("{orderID}/SendEmail")]
[Produces("application/json", Type = typeof(string))]
public ActionResult Post(int orderID)
{
return StatusCode(500, "Sorry!");
}
Here's the code in another app calling the API. I can get the status code number (500) using (int)getRespParams.StatusCode and the status code text (InternalError) using getRespParams.StatusCode, but how do I get the "Sorry!" message?
var getRespParams = await $"http://localhost:1234/api/Orders/{orderID}/SendEmail".PostUrlEncodedAsync();
int statusCodeNumber = (int)getRespParams.StatusCode;
PostUrlEncodedAsync returns an HttpResponseMessage object. To get the body as a string, just do this:
var message = await getRespParams.Content.ReadAsStringAsync();
One thing to note is that Flurl throws an exception on non-2XX responses by default. (This is configurable). Often you only care about the status code if the call is unsuccessful, so a typical pattern is to use a try/catch block:
try {
var obj = await url
.PostAsync(...)
.ReceiveJson<MyResponseType>();
}
catch (FlurlHttpException ex) {
var status = ex.Call.HttpStatus;
var message = await ex.GetResponseStringAsync();
}
One advantage here is you can use Flurl's ReceiveJson to get the response body directly in successful cases, and get the error body (which is a different shape) separately in the catch block. That way you're not dealing with deserializing a "raw" HttpResponseMessage at all.

RavenDB - deleting previously-conflicted documents with "/conflicts" in URL

I’m trying to programmatically delete once-conflicted documents that are no longer conflicted, but still showing as duplicates in Raven.
E.g. if I query an index on an entity’s property I know to be unique I get two documents back
Document A with URL entities/12345
Document B with URL entities/12345/conflicts/54321
My goal is to delete Document B.
Loading document A into a session does not throw a ConflictException, as it is not flagged as being conflicted any more. I can delete document B via the web UI, but can’t do it via code as yet, as I can only see it in transient context via a stream.
Here’s some sample code which explains what I am getting back from various client calls when trying to resolve this…
using (var enumerator = session.Advanced.Stream(query))
{
while (enumerator.MoveNext()))
{
var entity = enumerator.Current.Document;
// This attempt to get the id returns null
var id = session.Advanced.GetDocumentId(entity);
// Throws InvalidOperationException
var url = session.Advanced.GetDocumentUrl(entity);
// Returns null, so can’t use session to delete
session.Load<TEntity>(entity);
// Does nothing, with string ID of entity
session.Advanced.Defer(new DeleteCommandData { Key = entity.Id.ToString() });
// Does nothing
session.Advanced.DocumentStore.DatabaseCommands.Delete(entity.Id.ToString(), null);
}
session.SaveChanges();
}
Any help would be gratefully received!
I found that using enumerator.Current.Key in the example above gave me the key to the document I was after.

request.setAttribute is not working with chain.doFilter

I have two servlet ReplayFilter and VideoReplayServlet. From ReplayFilter, I am calling VideoReplayServlet using chain.doFilter. I am able to call VideoReplayServlet from ReplayFilter but I am not able to get userId variable from request object in VideoReplayServlet, which I have already set in request object before calling chain.doFilter. You can find my code below -
In ReplayFilter -
request.setAttribute("userId", userId);
request.setAttribute("uname", "mari");
chain.doFilter(request, response);
In VideoReplayServlet -
String uname = request.getParameter("uname");
String user_Id = request.getParameter("userId");
In VideoReplayServlet replay, I am getting both uname and user_Id null.
Can anybody help me?
I think the issue here is that you are setting it as an attribute and expecting it as a parameter which is contradicting.
Try the below code instead
request.getAttribute("userId", userId); //Note the getAttribute() instead of getParameter()

REST GET api - what to return when resource not found

I am using jersey to create rest api. I have GET api which returns xml or json representation of an object instance using JaXB. Everything is fine as long as I can get this instance based on id and return it. But when I don't find instance what should I return. I know 404 response has to be returned. But my method already returns a given type. So how do I setup 404 status is response?
Here is simplified version of my method
#GET
#Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public GameDAO getGameState(#PathParam("gameId") String gameId)
{
//code to get game instance based on gameId
if(game != null)
{
GameDAO d = new GameDAO(game);
return d; //gets auto converted to xml or json
}
return null; //how to return not found response ?
}
A 404 response is what you want, and I think the best way to get there is by throwing a "not found" WebApplicationException. Here's an example:
throw new WebApplicationException(Response.Status.NOT_FOUND);
There are plenty of ways to customize the error handling; you can find more details in the Jersey docs: https://jersey.java.net/documentation/latest/representations.html