Skip to content

[Android] Unable to handle error response coming from server #282

@Srikanthjava972

Description

@Srikanthjava972

Please, tell us how to recreate the issue in as much detail as possible.

when api fails with 400 status server is sending error response but I'm unable to read it, I'm able read response only in case of success.

Package.json

`

  "tns-android": {
    "version": "6.5.3"
  },
  "tns-ios": {
    "version": "6.5.4"
  }
},
"scripts": {
  "clean": "tns platform clean ios && tns platform clean android",
  "android": "tns run android --env.aot",
  "ios": "tns run ios --env.aot",
  "ios-prod": "tns run ios --env.aot --env.name=production",
  "android-prod": "tns run android --env.aot --env.name=production",
  "deploy": "./appcenter_deploy.sh ",
  "lint": "tslint \"src/**/*.ts\""
},
"dependencies": {
  "@angular/animations": "~8.2.0",
  "@angular/common": "~8.2.0",
  "@angular/compiler": "~8.2.0",
  "@angular/core": "~8.2.0",
  "@angular/forms": "~8.2.0",
  "@angular/http": "8.0.0-beta.10",
  "@angular/platform-browser": "~8.2.0",
  "@angular/platform-browser-dynamic": "~8.2.0",
  "@angular/router": "~8.2.0",
  "@ngx-translate/core": "^11.0.1",
  "@ngx-translate/http-loader": "^4.0.0",
  "@ngxs-labs/emitter": "^2.0.0",
  "@ngxs/store": "^3.6.1",
  "@nstudio/nativescript-cardview": "^1.0.0",
  "@nstudio/nativescript-checkbox": "^1.0.0",
  "@nstudio/nativescript-loading-indicator": "3.0.4",
  "@nstudio/nativescript-pulltorefresh": "^1.1.1",
  "ajv": "^6.10.0",
  "base-64": "^0.1.0",
  "bootstrap": "^4.3.1",
  "core-js": "3.4.1",
  "d3-ease": "^1.0.6",
  "i": "^0.3.6",
  "jquery": "^3.4.1",
  "moment": "^2.24.0",
  "nativescript-android-utils": "^1.0.2",
  "nativescript-angular": "^8.20.1",
  "nativescript-appavailability": "^1.3.2",
  "nativescript-appversion": "^1.4.2",
  "nativescript-audio": "^5.0.5",
  "nativescript-azure-mobile-apps": "^2.0.3",
  "nativescript-background-http": "4.2.0",
  "nativescript-camera": "^4.5.0",
  "nativescript-drawingpad": "^3.1.0",
  "nativescript-drop-down": "^5.0.4",
  "nativescript-email": "^1.5.5",
  "nativescript-exit": "^1.0.1",
  "nativescript-fingerprint-auth": "^7.0.2",
  "nativescript-geolocation": "^5.1.0",
  "nativescript-imagepicker": "^7.1.0",
  "nativescript-iqkeyboardmanager": "^1.5.1",
  "nativescript-local-notifications": "^4.1.5",
  "nativescript-mediafilepicker": "^3.0.1",
  "nativescript-microsoft-appcenter": "^2.0.0",
  "nativescript-permissions": "^1.3.8",
  "nativescript-phone": "^1.4.0",
  "nativescript-photo-editor": "^1.1.0",
  "nativescript-plugin-firebase": "^10.3.1",
  "nativescript-secure-storage": "^2.6.0",
  "nativescript-swiss-army-knife": "^4.0.2",
  "nativescript-theme-core": "^2.0.24",
  "nativescript-toasty": "^3.0.0-alpha.2",
  "nativescript-ui-calendar": "^6.0.0",
  "nativescript-ui-dataform": "^6.0.0",
  "nativescript-ui-listview": "^8.0.0",
  "nativescript-ui-sidedrawer": "^8.0.0",
  "npm": "^6.14.4",
  "popper.js": "^1.15.0",
  "reflect-metadata": "~0.1.12",
  "rxjs": "^6.4.0",
  "tns-core-modules": "^6.5.20",
  "zone.js": "^0.9.1"
},
"devDependencies": {
  "@angular-devkit/build-angular": "^0.901.0",
  "@angular/cli": "^8.3.18",
  "@angular/compiler-cli": "~8.2.0",
  "@nativescript/schematics": "~0.7.3",
  "@ngtools/webpack": "~8.2.0",
  "@types/d3-ease": "^1.0.9",
  "@types/jasmine": "2.8.6",
  "@types/jasminewd2": "~2.0.3",
  "@types/node": "12.6.3",
  "codelyzer": "^5.0.1",
  "nativescript-dev-webpack": "^1.5.1",
  "node-sass": "^4.13.1",
  "protractor": "^6.0.0",
  "tns-android": "6.5.3",
  "tns-ios": "6.5.4",
  "tns-platform-declarations": "6.4.2",
  "ts-node": "8.5.0",
  "tslint": "5.18.0",
  "typescript": "3.4.5"
},

}
`

muitipart-handler.service

`public multipartUpload(url: string, token: string, bgSessionName: string,
params: any, androidNotificationTitle: string = 'Sync complete') {

    const session = bgHttp.session(bgSessionName);
    const name = 'test';
    const description = `${name} (${++this.counter})`;
    const request = {
        url: url,
        method: 'POST',
        headers: {
            Authorization: this.apiHelper.getAuthHeaderAsString(token),
            'Content-Disposition': 'form-data; name=' + name + 'filename=' + name,
            'Content-Type': 'application/octet-stream'

        },
        description: description,
        androidAutoDeleteAfterUpload: false,
        androidNotificationTitle: androidNotificationTitle,
        androidAutoClearNotification: true
    };

    setTimeout(() => { // Timeout of 20 seconds. If the upload doesn't complete in 20 seconds. it will return a 408.
        if (!this.finishedUploading) {
            this.response.next({responseCode: 408, data: 'Multipart upload timed out'});
        }
    }, 20000);

    const task = session.multipartUpload(params, request);
    this.bindTask(task);
    return this.response;
}

/**
 * Bind the task to the events. This will create callbacks for every stage of the call.
 * (responsed, progress, complete, error)
 *
 * To get the response code and data from the response, please subscribe on MultipartHandlerService.response
 *
 * @param task - bgHttp.Task
 */
private bindTask(task: bgHttp.Task): any {
    task.on('progress', this.onEvent.bind(this));
    task.on('error', this.onEvent.bind(this));
    task.on('responded', this.onEvent.bind(this));
    task.on('complete', this.onEvent.bind(this));
    this.lastEvent = '';
}

private onEvent(e): Observable<boolean> {
    if (this.lastEvent !== e.eventName) {
        // suppress all repeating progress events and only show the first one
        this.lastEvent = e.eventName;
    } else {
        return;
    }
    if ( e.eventName === 'complete') {
        console.log('completehandler')
        this.finishedUploading = true;
        this.response.next({responseCode: e.responseCode, data: JSON.parse(this.data)});
    }

    if ( e.eventName === 'responded') {
        console.log('respondhandler')
        this.data = e.data;
        console.log(e.data)
    }

    if ( e.eventName === 'error') {
        console.log(`Multipart HTTP error.
                    ResponseCode: ${e.responseCode},
                    Response: ${e.response}`);
    }

    this.events.push({
        eventTitle: e.eventName + ' ' + e.object.description,
        eventData: JSON.stringify({
            error: e.error ? e.error.toString() : e.error,
            currentBytes: e.currentBytes,
            totalBytes: e.totalBytes,
            body: e.data,
            responseCode: e.responseCode
        })
    });
}

}
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions