getx icon indicating copy to clipboard operation
getx copied to clipboard

GetConnect trying to cast the response to the type of decoder in case of 500 status

Open AbhijithKonnayil opened this issue 4 years ago • 4 comments

Describe the bug GetConnect trying to cast the response to the type of decoder in case of 500 status. But in the case of 500, the response may be of type 'text/html'. The response modifier is called after this. So we cant handle this error before that. I assume the problems is in the file lib>get_connect>http>utils>body_decoder.dart line 22 - 32

  try {
    if (stringBody == '') {
      body = null;
    } else if (request.decoder == null) {
      body = bodyToDecode as T?;
    } else {
      body = request.decoder!(bodyToDecode);
    }
  } on Exception catch (_) {
    body = stringBody as T;
  }

**Reproduction code

class UserProvider extends BaseProvider {
  @override
  void onInit() {
    httpClient.defaultDecoder = (map) {
      return User.fromJson(map);
    };
    super.onInit();
  }


  Future<User?> onLogin(
      {required String username, required String password}) async {
    try {
      Response<User> response = await post<User>(
          'login/', {'username': username, "password": password});
      return response.body;
    } catch (e) {
      throw (AtException(e));
    }
  }
}```

**To Reproduce**
Steps to reproduce the behavior:
1. add a default model decoder to the HTTP client in GetConncet sub-class
2. . Make the server break the return 500 status in text/html fromat.

**Expected behavior**
The response modifier should execute before the casting occurs. 


**Flutter Version:**
Flutter 2.5.3

**Getx Version:**
get: ^4.5.1

**Describe on which device you found the bug:**
ex: Asus Zenfone Max Pro M1



AbhijithKonnayil avatar Dec 03 '21 20:12 AbhijithKonnayil

The responseModifier should be called before casting or executing the result with the decoder. In some cases, the JSON returned in case of error is in a different form and cant be processed by the decoder.

AbhijithKonnayil avatar Jan 05 '22 06:01 AbhijithKonnayil

If you have a decoder specified, the try catch never occurs, how to do it the right way? example: ``` try { final Response res = await post<List<OPInfoModel>>( '${baseProvider.nodeApiUrl}/execDBQuery', json.encode(paramssend), contentType: "application/json", decoder: (data) { return data?.map<OPInfoModel>( (d) => OPInfoModel.fromMap(d), )?.toList() ?? []; }, ); return res.body; } catch (e) { log('Erro api getOPInfo: ${e.toString()}'); }

GnRSlash avatar Jan 28 '22 17:01 GnRSlash

I think, keep current response modifier for success case and add some error handler or just try catch with the bodyDecoded call

https://github.com/jonataslaw/getx/blob/b797ae2b5c9039a00fd4837c6cba10751462da9a/lib/get_connect/http/src/http/io/http_request_io.dart#L62-L76

bungabear avatar Mar 25 '22 11:03 bungabear

this really needs to be addressed..

wheeOs avatar Jul 08 '22 15:07 wheeOs