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 :: remove json javascript 
Javascript :: javascript object without undefined values 
Javascript :: mongoose put request 
Javascript :: javascript backwards loop array 
Javascript :: regex js pattern tags 
Javascript :: routes in node js 
Javascript :: how to submit form on changed url in function in jquery 
Javascript :: sanitizer content nodejs 
Javascript :: str replace javascript all 
Javascript :: sorting in javascript 
Javascript :: How to add Strings as numbers in JavaScript 
Javascript :: node js event emitter 
Javascript :: node.js ping 
Javascript :: rgb javascript 
Javascript :: list of higher-order functions javascript 
Javascript :: namespace in javascript 
Javascript :: open modal useState 
Javascript :: generate numbers from 1 to 100 to array 
Javascript :: javascript slice vs splice 
Javascript :: fetch get request 
Javascript :: discord.js guildMemberRemove 
Javascript :: filter repetition multidimensional array javascript 
Javascript :: vue toggle boolean on click 
Javascript :: radio button getelementsbyname 
Javascript :: how to import svg in react 
Javascript :: generate random color array javascript 
Javascript :: how to loop through something in node.js 
Javascript :: find property in nested object 
Javascript :: aes 256 nodejs 
Javascript :: change href javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =