I am using react-admin framework and I am trying to get error status code (404,500), which I would be saving to some variable for later usage. For example, when I try to create an user that has same e-mail address as already created user, the server wont allow this operation and the request fails with status code 500.
I need that status code to save to variable and work with it later.
Does react-admin offer this?
So far I have tried this simple code in my custom DataProvider, but when logging it to console, it returns undefined.
const status = params.message.status;
I wasn't able to find any other solution that I would fully understand.
Any ideas how to solve this?
Thank you in advance.
Basically this is what are you looking for:
https://developer.mozilla.org/en-US/docs/Web/API/Response/status
First of all, you actually need to request some data from the server in order to get the response.status code.
The most simple example would be this based on the link I listed before:
var myRequest = new Request(options.body); //depends on what do you want to request
fetch(myRequest).then(function (response) { //gets the response from server
console.log(response.status, 'status'); // this returns the status value
});
This should return the response.status code.
Related
I have a Component in my react.js application, which display an error code.
Like this:
Now I'm displaying just a code, along with this what I want to to is - show the reason of this error too. Like this page contains a lot of information about error codes.
How can I do it ?
If I get any error, then I get the error code and use window.location="/bad_request/".concat(err_code);
In my ErrorPage component I've :
componentWillMount() {
this.state.errorCode = this.props.match.params.errorCode;
console.log(this.state.errorCode);
}
Rest is simple.
In common, HTTP response codes are being used for describing a general result of the response. For example, server should return one of 20x codes (200, 201, 202, ...) on some success operation or 40x (400, 401, 401, ...) on client error etc.
That said, code itself won't describe an actual request result in details so you have to do consider an error code only like general reference point.
Answering to you question, I'd suggest you need to take a following steps:
1. Review your server response structure to make it include an actual error message in response body.
2. Review your component structure to retrieve error message from response body to display it (or perform any other required actions)
Basically this error indicating that server didn't receiving complete req. in its time period So it is giving this error So you use increase server timeout as well as you can check with sending less data on server. For more info:
https://airbrake.io/blog/http-errors/408-request-timeout
I wrote this to my java-script file :
let map = new Map();
map.set(400,'Bad Request');
map.set(401,'Unauthorized');
...
When I get redirected to this page, I just get what was the description of the error.
Now I have entered all the data manually, I was just asking if there was any automated way to log these.
We need to send an HTTP CODE = 200 with a body 'OK' in reply to a notification through Zapier.
Is it possible to use the following code in Zapier:
var http = require('http');
const server = http.createServer((req,res) => {
res.statusCode = 200;
res.end('OK');
}).listen(80);
It returns an error:
Error: You did not define `output`! Try `output = {id: 1, hello: "world"};`
And the reply doesn't work.
David here, from the Zapier Platform team.
To cut to the chase - though it might be possible to start an http server (there's no reason it wouldn't be, as far as I know), it's not going to do what it seems like you're hoping to do. Namely, you can't send a custom response to an incoming webhook. From the docs:
There is no way to customize the response to the request you send to the Catch Hook URL, as the response is sent before the Zap triggers and runs on the webhook request.
If you need behavior like that, I'd suggest running a webserver.
The specific Code step error you're seeing has to do with not defining output to the function. Something goes in and something must come out. You can customize the output based on the input and use that output, but something has to be returned from the function (even if it's just {}).
I am doing "window.open(file_url)" to download a file and if a file exists backend returns a Blob which gets downloaded by the browser but if the file doesn't exist then backend returns a JSON error message with request status as 500.
so is there is some way to know that "status" for a page.
I know for AJAX we get the status property but for normal web pages do we have some way to know that status since when the browser makes a request for a page its an HTTP GET and it should have status.
This is a working example code. So, you should use it.
$.get(url, function(data,status, xhr) {
alert(xhr.status);
});
You can check for error as
var test = window.open(file_url)
test.onError = alert('The specified file was not found. Has it been renamed or
removed?');
I'm using Twilio.Device in an Angular app and I need to display a message if the call failed due to an invalid number. I know that you can call Twilio's REST API to get the call status, but is there a way to get the status without making that extra call?
For example, I was hoping that the connection object that you get back in the disconnect handler would give you the status, like this:
Twilio.Device.disconnect(function(connection) {
if (connection.status === 'failed') {
// display error message
}
});
However, that's either not possible or I'm not looking in the right place.
Another idea I had is when I set debug to true, I can see this helpful log when making a call with an invalid number:
[Connection] Received HANGUP from gateway
[Connection] Disconnecting...
But is there any way to access that HANGUP event?
Thanks in advance!
I'm not sure exactly which you'd need, but in the accept or connect methods, try listening for the 'hangup' or 'error' events which are emitted by the Connection class:
Twilio.Device.connect(function(connection) {
connection.on('hangup', function (err) {
console.log(err)
})
})
Twilio developer evangelist here.
You can actually get all the details that you would normally need the Twilio REST API for on the Twilio.Connection object. Just take a look at the parameters attribute, it contains all the normal Twilio voice request parameters including CallStatus.
Using a normal html form, I can add a new task by sending POST request to /api/tasks/insert/
POST data includes $name and $description of the task.
However, when I use Angular to push the data to REST API in php, only a POST request is sent and an empty row is created in the database.
This means that the POST variables are not being passed i.e. name and description.
What am I doing wrong?
I have been stuck at this for the last few hours now. I have checked countless tutorials and am pretty sure of the syntax. My backend REST api in PHP works fine.
var res=$resource('http://localhost/api/tasks/insert/',{},
{
createTask:{method:'POST'}
});
postData={name:"Hello",description:"DescBaby"}
res.createTask({},postData);
//res.createTask(postData); tried this also, but doesn't work
Another variation that I tried based on an comment was this:
res.createTask({name:"TestName", description:"descBaby"}).$promise.then(function(value)
{
console.log("Success"); //I get success in console.
},function(errResponse)
{
console.log("Error");
});
Angular Does not give me any errors. Just sends a blank POST request to the url.
EDIT:
I checked in the network pane in Chrome whether the data was sent or not and as it turns out it is being sent.
However, in the response it's showing this :
Undefined index: name in XYZ.php line ABC.
The line pointed above is the following line in my PHP:
$obj->InsertTask($_POST['name'],$_POST['description']);
Thanks to a friend of mine, I finally got the code running. The problem wasn't with my Angualar Code but with php code.
As it turns out I cannot read POST data as $_POST[].
file_get_contents should be used in such cases as data is sent through a JSON payload and not through request parameters!
Here's how I got it running : Angularjs $http.post, passing array to PHP
Did you look at the network tab of debugger? You can check if valid data was sent to server. If it was ok this is not JS or Angular question. On the first look it seems that you have valid code for angular. Just in case check if you have set proper ajax headers.
"X-Requested-With": "XMLHttpRequest"
Does other AJAX calls work for you?
Try this (and create a factory to follow the best practices):
Service
services.factory("Tasks", function($resource) {
return $resource('http://localhost/api/tasks/insert/', {}, {
createTask: {method:'POST', params: {name:"Hello",description:"DescBaby"}}
})
});
Controller
Tasks.$createTask();