Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to get error using fetch API

function handleErrors(response) {
    if (!response.ok) {
        throw Error(response.statusText);
    }
    return response;
}

// then use as below
fetch("http://httpstat.us/500")
    .then(handleErrors)
    .then(response => console.log("ok") )
    .catch(error => console.log(error) );
Comment

how to get error using fetch API

function CheckError(response) {
  if (response.ok) {
    return response.json();
  } else {
    throw Error(response.statusText);
  }
}

// Now call the function inside fetch promise resolver
fetch(url)
  .then(CheckError)
  .then((jsonResponse) => {
  }).catch((error) => {
  });
Comment

Basic fetch with error

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h1>Test Page</h1>
    <!-- https://youtu.be/YJ7ZgGnhN5k by Steve Griffith -->
    <script>const root = 'http://jsonplaceholder.typicode.com';//url for fetch
        let id = Math.floor(Math.random() * 20) + 1;//random number. If >10, an error will be received, otherwise the data will be logged to the console
        let uri = root + '/users/' + id;
        console.log('Fetch: ', uri);

        fetch(uri).then(function (response) {
            if (response.status == 200) {
                return response.json();
            }
            else { throw new Error('Number is greater than 10') }
        }).then((data) => {
            console.log(data);
            let jsonData = JSON.stringify(data);
            console.log(jsonData);
        }).catch((err) => {
            console.log("Error: ", err.message);
        });</script>
</body>

</html>
Comment

PREVIOUS NEXT
Code Example
Javascript :: js return vs break in for loop 
Javascript :: expect(insTypeDB).is.to.equals(61) on javascript 
Javascript :: aurelia shadow dom 
Javascript :: ES6 template literals sum example 
Javascript :: find duplicate characters from string in javascript 
Javascript :: javascript map api key insert 
Javascript :: diagonal difference javascript 
Javascript :: sus 
Javascript :: verificar radio selected 
Javascript :: compile pdf with javascript 
Javascript :: JavaScript place auto complete 
Javascript :: ionic vue electron 
Javascript :: how to add some thing in JS Array 
Javascript :: dynamic operator javascript if statement 
Javascript :: VueJs System Modifier keys like exact ctrl alt shift meta 
Javascript :: how to get all key values of Json 
Javascript :: how to change selected link in jquery 
Javascript :: react native biometrics sign in 
Javascript :: turn gravity off on one object matter js 
Javascript :: setting a date range using yup on react date picker 
Javascript :: function return string javascript 
Javascript :: remove property from query string javascript 
Javascript :: how to get query data using javascript 
Javascript :: react foreach loop 
Javascript :: React Native - Trigger Media Scanner 
Javascript :: jboss-ejb-client.propeties exampe de configuration 
Javascript :: javascript id generator 
Javascript :: on inline style change 
Javascript :: jquery get selected checkbox items and pass to parameter for C# MVC consumption 
Javascript :: return array odd or even outlier 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =