Rethrow Exception from slack api Response payload, or handle it appropriately - kotlin

The Slack conversations.join api returns a Slack API Exception (part of Response Body - i.e ConversationsJoinResponse object returned from Slack Library response) when it is unable to join a slack channel for some reason , it is sent as a part of API Response object which is a ConversationsJoinResponse object .
I have used this Api which returns the Slack Library Response object.
Documentation - https://api.slack.com/methods/conversations.join
Api - https://slack.com/api/conversations.join
IMHO it would be better to throw the exception I received from the Api itself.
So I am trying to rethrow the same exception I get from the slack library and returns the error message in Response object returned by the library i.e ConversationsJoinResponse object.
Would this be the right way to handle the exception where the SlackApiException class takes a Request Body and a message.
logger.error("Error Joining Channel - $channelId : ${response.error}")
throw SlackApiException(
Response.Builder().code(400).request(
Request.Builder().url("https://slack.com/api/conversations.join").build())
.message(response.error).build(),
response.error
)
Any help with figuring the appropriate way to handle the Api Response is appreciated.

Related

Bypassing Play's HttpErrorHandler for 4xx errors

I'm writing a microservice in Play. I'd like my controller to be able to generate client errors (4xx) with a particular JSON response body. However, Play's default HttpErrorHandler kicks in, and replaces my response body with an HTML document.
How can I have my response returned to the client untouched?
I have looked into providing a custom HttpErrorHandler, but this doesn't give access to the response that my controller had generated; the signature is:
def onClientError(request: RequestHeader, statusCode: Int, message: String): Future[Result]
Edit: I can no longer reproduce this problem. Now, the error handler doesn't kick in -- which is the behaviour I'd expect. Most likely some form of user confusion / error.
A client error is a condition which is caused by the client, and Play doesn't know how to handle. That includes malformed headers, non-existing resources (read : No route available for that path).
In all cases, this won't hit a controller : It's handled before it's routed. That also means there is no body that can be passed along.
If it does hit a controller, you're free to return a Result with the proper response code and body. If it doesn't hit a controller, and the error handler is invoked, you need to return a response based on the request itself.
An example of what you're trying to achieve would be handy, since it's a bi t unclear to me.

HttpClient Error getting response stream (ReadDone1): ReceiveFailure

I am using HttpClient class to make a web request to get data from server( httpClient.GetAsync(url, HttpCompletionOption.ResponseContentRead). But randomly i get this error saying Error getting response stream (ReadDone1): ReceiveFailure I have a try catch block around this call but it does not get caught. Instead I get the error in UIApplication.Main(args, null, "AppDelegate"); in the IOS App. Is there any way to catch this error or to fix this error?
Are you using ModernHttpClient? If not, give it a shot. It's available as a component and on github. It's a drop in replacement for HttpClient that performs a lot better.

eBay API GetFeedback call returns 'Input data for tag <GetFeedback> is invalid or missing' error

I try to get all feedback (CommentType [positive,neutral,negative], user who left, date, message) for the particular user in xml format. For this I use http request:
https://api.ebay.com/wsapi?callname=GetFeedback&UserID=pashtetgp1988&siteid=0&DetailLevel=ReturnAll&appid=eBayAPID-73f4-45f2-b9a3-c8f6388b38d8&version=511
However, xml output returns error
Input data for tag <GetFeedback> is invalid or missing. Please check API documentation.
Another request with the same appid works fine:
http://open.api.ebay.com/shopping?callname=GetUserProfile&version=537&siteid=0&appid=eBayAPID-73f4-45f2-b9a3-c8f6388b38d8&UserID=pashtetgp1988&IncludeSelector=Details,FeedbackHistory&responseencoding=XML
What can be the problem?
GetFeedback is from of the eBay Trading API Service and you cannot make calls to this service using HTTP GET. The service only supports requests made by HTTP POST. Information required by GetFeedback is passed either through the HTTP headers or the body. The body of the request can be XML or SOAP. More information is available in the eBay documentation.
For future reference you should never expose your appid in code examples.

REST API request failed http response code

I am implementing a REST API and i have a set_ftp_credentials request
for example
POST api.domain.com/object/set_ftp_credentials
This request checks that the ftp credentials are good by trying to connect.
We had a discussion here whether we should return a HTTP response 200 and in the content
return the the request have failed and the reason.
Or we should return some other HTTP response code (40* response code).
What is considered the right way of doing this ?
And if not 200 then what do you think is the right response code ?
I believe that 200 is an OK response and should only be sent when the request was successfully satisfied.
One approach would be to answer the following questions for yourself:
What HTTP methods are you supporting?
What is the expected behavior of each HTTP method?
For each supported HTTP method, What are the possible classes of failures that can result if something goes wrong?
Now, return your expected response pay load with a 200 HTTP response code. Return client side error (400 class) or server side error (500 class) for the failures.
You might want to consider the following:
Return error codes that are specific to your application and map them to HTTP error codes
Have separate data contracts (assuming JSON format or XML schema) defined for successful response and error responses

WebFaultException to pass back an XML message rather than string?

In my RESTful services layer, any exceptions that bubble up to me are caught as Fault Exceptions. Within that FaultException, there's a custom XML message that contains <errorNumber>, <errorCode> and a <message>. I re-package the exception as a WebFaultException so I can set the HttpStatusCode for the response header to 400, 401, 404, etc.
However, I don't want to use WebFaultException<string>(string message, HttpStatusCode code). I want the message to also be an XML message.
Anyone seen how to set the HttpStatusCode of the response message AND set an XML message? I'm using Fiddler to examine my response headers and any messages that come up from the service.
What I did to get around this was create a new class MyException with simple properties and used WebFaultException<MyException> and it works nicely. I found the solution at the following link: http://www.c-sharpcorner.com/UploadFile/ankithakur/ExceptionHandlingWCF12282007072617AM/ExceptionHandlingWCF.aspx