VB10 / vexana

Vexana is network manager project with dio.
https://pub.dev/packages/vexana
MIT License
146 stars 41 forks source link

ErrorModel Data Incorrect #111

Closed burakJs closed 1 month ago

burakJs commented 2 months ago

I have the same model for both successful and error cases

@JsonSerializable(createToJson: false, fieldRename: FieldRename.pascal)
final class AppErrorModel extends INetworkModel<AppErrorModel> {
  const AppErrorModel({
    this.title,
    this.detail,
    this.status,
  });

  final String? title;
  final String? detail;
  final int? status;

  factory AppErrorModel.fromJson(Map<String, dynamic> json) => _$AppErrorModelFromJson(json);

  @override
  AppErrorModel fromJson(Map<String, dynamic> json) => _$AppErrorModelFromJson(json);

  @override
  Map<String, dynamic>? toJson() => {};
}
abstract class BaseResponseModel<T> extends INetworkModel<T> {
  const BaseResponseModel({this.success = false, this.message, this.errorModel});

  final bool success;
  final String? message;
  final AppErrorModel? errorModel;
}
@JsonSerializable(createToJson: false, fieldRename: FieldRename.pascal)
final class LoginResponseModel extends BaseResponseModel<LoginResponseModel> {
  final LoginResponseDataModel? data;

  const LoginResponseModel({
    this.data,
    super.success,
    super.message,
    super.errorModel,
  });

  factory LoginResponseModel.fromJson(Map<String, dynamic> json) => _$LoginResponseModelFromJson(json);

  @override
  LoginResponseModel fromJson(Map<String, dynamic> json) => _$LoginResponseModelFromJson(json);

  @override
  Map<String, dynamic>? toJson() => {};
}

Since it is like this, I need to be able to receive my model in case of error. That's why I edited the receiveDataWhenStatusError and validateStatus values

NetworkManager _baseNetworkManager() => NetworkManager<EmptyModel>(
        isEnableLogger: true,
        options: BaseOptions(
          baseUrl: baseUrl,
          contentType: ContentType.json.value,
          receiveDataWhenStatusError: true,
          validateStatus: (status) => status! < 500,
        ),
      );

When I get an error, I cannot get the status I expect because of the code I specified below in the send method in the package.

return ResponseModel(
        error: ErrorModel(description: response.data.toString()),
      );
behzodfaiziev commented 2 months ago

@burakJs as one of the solutions we can throw a DioException so it will be handled by catch case. image

burakJs commented 2 months ago

@burakJs as one of the solutions we can throw a DioException so it will be handled by catch case. image

It seems logical. I will open a PR for this and then test it in my project and share the result here. Thank you very much for the solution suggestion

burakJs commented 2 months ago

I tried this solution and i got error model in response.error.model thanks for it @behzodfaiziev