簡體   English   中英

Google Cloud Endpoints自定義例外

[英]Google Cloud Endpoints Custom Exception

我有以下方法引發異常:

@ApiMethod(name = "login")
public Profile getLogin(User user) throws UnauthorizedException {

    if (user == null){
        throw new UnauthorizedException("missing user");
    }

    ...
}

如何捕獲android客戶端AsyncTask中的錯誤?

protected Profile doInBackground(Void... unused) {

    Profile profile = null;
    try {
        profile = service.login().execute();

    } catch (Exception e) {
        Log.d("exception", e.getMessage(), e);
    }

    return profile;
}

上面沒有捕獲UnauthorizedException。

例外情況發生在App Engine服務器上,而不是Android客戶端上。 然后,您必須找到一種向客戶端發送消息的方法,以告訴他發生了什么異常。

在Cloud Endpoints和其他REST API中,這是使用HTTP結果代碼和HTTP結果消息完成的。

Cloud Endpoints文檔說明了如何將自定義異常與HTTP結果代碼進行匹配 但是,在您的情況下,您還可以使用提供的com.google.api.server.spi.response.UnauthorizedException 這將轉換為HTTP 401代碼,這是HTTP說“未授權”的一種方式。

我從來沒有在Android上嘗試過Cloud Endpoints,但是如果你檢查異常類,你肯定會發現有一種方法可以獲取HTTP錯誤代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM