Giter Site home page Giter Site logo

Comments (3)

edgurgel avatar edgurgel commented on May 3, 2024

Hi @ianwalter, the :ok tuple means: this request didn't fail (connection was not closed, it reached the server, the whole request was transmitted, etc).

The status code is dependent on the application. It may seem clear that 404 is an error, but what about a 303 or a 301? Should they be error or ok?

You may even have APIs that always return 200 and putting some error status inside the JSON payload. For example:

$ curl -i "http://api.openweathermap.org/data/2.5/weather?id=21372797&appid=2de143494c0b295cca9337e1e96b00e0"
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 08 Nov 2015 05:32:04 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Source: redis
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST

{"cod":"404","message":"Error: Not found city"}

from httpoison.

lgraziani2712 avatar lgraziani2712 commented on May 3, 2024

HTTP/1.1 200 OK

That 200 is extrange. Why don't send a 404 error? With that 200 OK response, you have to handle errors in the success function.

What actually I have to do:

import axios from 'axios';

const OPEN_WEATHER_MAP_URL = 'http://api.openweathermap.org/data/2.5/weather?appid=XXXXXXXXXXXXX&units=metric';

export default {
  getTemp : function getTemp(location) {
    var encodedLocation = encodeURIComponent(location),
    requestUrl = `${OPEN_WEATHER_MAP_URL}&q=${encodedLocation}`;

    return axios.get(requestUrl).then(function success(res) {
      // I have to check inside the success function
      if (res.data.cod && res.data.message) {
        throw new Error(res.data.message);
      }
      return res.data.main.temp;
    }, function error(res) {
      throw new Error(res.data.message);
    });
  },
};

What could be better:

import axios from 'axios';

const OPEN_WEATHER_MAP_URL = 'http://api.openweathermap.org/data/2.5/weather?appid=XXXXXXXXXXXXX&units=metric';

export default {
  getTemp : function getTemp(location) {
    var encodedLocation = encodeURIComponent(location),
    requestUrl = `${OPEN_WEATHER_MAP_URL}&q=${encodedLocation}`;

    return axios.get(requestUrl).then(function success(res) {
      return res.data.main.temp;
    }, function error(res) {
      // Since the header HTTP response is 404, it fails
      throw new Error(res.data.message);
    });
  },
};

It can be possible to you to have a "standard" http header for errors like that? Errors who are not related a malfunction.

Thanks!

from httpoison.

edgurgel avatar edgurgel commented on May 3, 2024

The open weather API is simply wrong. You should not reply 200 when the requested resource doesn't exist. I can't see how the client (HTTPoison) can protect anyone from such broken API.

from httpoison.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.