How can I know the type of error and response code from IOWebSocketChannel - flutter

I am trying to know if the connection error is Connection to 'some URL' was not upgraded to websocket. Also identify response code. The server is emitting 401.
I need this to know if I need to refresh the token and then reconnect.
final channel = IOWebSocketChannel.connect(Url)
final sub = channel.stream.listen((data){
//process data
},
onError: (error){
//confirm this error failing to upgrade and
// response code is 401
// then refresh token and reconnect
})

To know the error type
print('error type is ${error.runtimeType}');
then you can handle it
if(error is errorType)
{
...
}

Related

Flutter requesting API

I have a problem regarding fetching data or using the request API in local in flutter.
I tried to send a request using postman and it works, but when I'm trying to request in flutter, it gives me Connection refuse.
I'm using http package of flutter
This is the error, but i also tried jsonplaceholder api and it works.
I/flutter (10134): 5004/profile URI :>> http://127.0.0.1:5004/profile/v1.0/channels/1?includes=createdByProfile,joinedProfiles
I/flutter (10134): error :>> Connection refused
Heres my code:
Future loadData() async {
try {
var data = await http.get(Uri.parse('http://127.0.0.1:5000/content/v1.0/contents'));
print("data!! :>> $data");
} catch (error) {
print("ERROR! $error");
}
}
it always go to catch and says Connection Refused. But when i tried to request in postman and access the url in browser, it shows the data there

How get api error message in Flutter dart

Is it possible get error message when call rest api in flutter?
Eg.
print(response.statusCode); --return code
How print this error message?
The request could not be processed because an error occurred whilst attempting to evaluate the SQL statement associated with this resource. Please check the SQL statement is
If you are using dio package you can catch DioError and fetch a response from the error.
In my case back returns me the message in ['error'][''message].
I'm not sure how it works with http package. Firstly you can print e.response!.data to see the structure and then get the error text
try {
//make request
} on DioError catch (e, stacktrace) {
final result = e.response!.data as Map<String, dynamic>;
print(result['error']['message']); <---- getting error message from response
}

Is it possible to know that the error is CORS in Axios?

Axios gives us the interception ability. I have created a response interceptor to get errors.
This is my code:
const errorInterceptor = error => {
if (error.code === 'ERR_NETWORK') {
throw new Error('Network is not connected')
}
// The rest of the code
}
However, if I get the CORS error, I can't find any information to know that it was a CORS error.
Why do I need this?
I want to provide meaningful messages to my users.
If network is disconnected, I want to show You are not connected to the internet. If it's CORS, I want to show API is not configured properly for CORS, please inform the administrator.
How can I know whether the error is CORS or not?
I have created an interceptor and I have tried to extract data from it.
axios.interceptors.response.use((response) => response, (error) => {
if (typeof error.response === 'undefined') {
alert('A network error occurred. '
+ 'This could be a CORS issue or a dropped internet connection. '
+ 'It is not possible for us to know.')
}
return Promise.reject(error)
})

PlatformException(400, HTTP status code error., null)

I am trying to download an image from an api, unforunately it showed an error. Changing the url works perfectly. How can I solve the problem? Is there any alternative way I can download this type of image?
Here's the code I am using for downloading the image:
_downloadandSaveImage() async {
try {
showdownloading = true;
// Saved with this method.
print(widget.imageUrl);
print(_progress);
//var imageId = await ImageDownloader.downloadImage(widget.imageUrl);
var imageId = await ImageDownloader.downloadImage("http://wallpaper.pkappstudio.info/upload/47418_animal2.jpg",
destination: AndroidDestinationType.directoryDCIM
);
Toast.show("Saved!", context, duration: Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);
if (imageId == null) {
return;
}
} on PlatformException catch (error) {
print(error);
}
}
i just had to turn usesClearTexttraffic to true in my androidManifest.xml file.
The error is coming from the link itself. It means that the link is invalid. It returned a status code of 400. This is what http status code 400 means:
The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
Credits to mozilla for their great explanation of status code 400: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400
Check this out so you will be able to handle other status codes that you might encounter in the future: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

Getting server response code in oracle jet web service call

I am calling a REST Webservice from Oracle jet ViewModel. The server response as I expected, but how to catch the server response (If the server response is like 400,422). I tried with the following lines of code, but it doesn't seem to be working.
self.User = oj.Model.extend({
urlRoot : self.resourceUrl,
idAttribute : "userId"
});
var user = new self.User();
user.save(
{
success: function(user, response, options) {
console.log("response "+response);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("error thrwos "+errorThrown);
console.log("status "+textStatus);
}
});
All I want to do is, if server response is a success, show the user a success message and navigate to the next page and if the response is an error( 400 or 422 or whatever), show the user an error message ( this can be done using a validator).
Looking at the JSDocs for model.save http://www.oracle.com/webfolder/technetwork/jet/jsdocs/oj.Model.html#save
You will see that you can define a callback function to handled the error returned by the save call.
This would work with what #Koshinae is saying in his comment above about options.