Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip out console.log for "This response was unable to be parsed as json" in production #430

Closed
LuisAverhoff opened this issue Jan 11, 2020 · 2 comments

Comments

@LuisAverhoff
Copy link

LuisAverhoff commented Jan 11, 2020

I'm using ember-fetch with ember data and I have a custom method in one of my adapters that returns csv instead of json.

The function determine-body-promise seem to emit a warning message in both production and development and is not ideal to have my entire csv payload, or any payload to be frank, be printed out in the console in production(development is fine).

My suggestion is the following.

// Add this line. You'll probably need to install ember-cli-babel as mentioned in
// https://github.com/emberjs/ember.js/issues/18062
import { DEBUG } from '@glimmer/env'; 

/**
 * Function that always attempts to parse the response as json, and if an error is thrown,
 * returns `undefined` if the response is successful and has a status code of 204 (No Content),
 * or 205 (Reset Content) or if the request method was 'HEAD', and the plain payload otherwise.
 */
export default function determineBodyPromise(
  response: Response,
  requestData: JQueryAjaxSettings
): Promise<object | string | undefined> {
  return response.text().then(function(payload) {
    let ret: string | object | undefined = payload;
    try {
      ret = JSON.parse(payload);
    } catch (error) {
      if (!(error instanceof SyntaxError)) {
        throw error;
      }
      const status = response.status;
      if (
        response.ok &&
        (status === 204 || status === 205 || requestData.method === 'HEAD')
      ) {
        ret = undefined;
      } else if(DEBUG) { // Check if we are in development mode.
        console.warn('This response was unable to be parsed as json.', payload);
      }
    }
    return ret;
  });
}
@nlfurniss
Copy link
Collaborator

Seems good, can you put up a PR @LuisAverhoff?

@nlfurniss
Copy link
Collaborator

Closed via #636

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants