1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-06-26 10:36:21 +02:00

api error detection updates

This commit is contained in:
Kyle Spearrin 2019-04-19 09:11:17 -04:00
parent 0c93fc2662
commit 8c8fa8ae4c
6 changed files with 52 additions and 8 deletions

View File

@ -1,6 +1,7 @@
using System.Threading.Tasks;
using Android.App;
using Bit.App.Abstractions;
using Bit.Core.Enums;
using Plugin.CurrentActivity;
namespace Bit.Droid.Services
@ -10,6 +11,8 @@ namespace Bit.Droid.Services
private ProgressDialog _progressDialog;
private Android.Widget.Toast _toast;
public DeviceType DeviceType => DeviceType.Android;
public void Toast(string text, bool longDuration = false)
{
if(_toast != null)

View File

@ -1,9 +1,11 @@
using System.Threading.Tasks;
using Bit.Core.Enums;
using System.Threading.Tasks;
namespace Bit.App.Abstractions
{
public interface IDeviceActionService
{
DeviceType DeviceType { get; }
void Toast(string text, bool longDuration = false);
bool LaunchApp(string appName);
Task ShowLoadingAsync(string text);

View File

@ -64,7 +64,7 @@ namespace Bit.App.Services
public Core.Enums.DeviceType GetDevice()
{
return Device.RuntimePlatform == Device.iOS ? Core.Enums.DeviceType.iOS : Core.Enums.DeviceType.Android;
return _deviceActionService.DeviceType;
}
public string GetDeviceString()
@ -91,7 +91,7 @@ namespace Bit.App.Services
else
{
var launched = false;
if(Device.RuntimePlatform == Device.Android && uri.StartsWith("androidapp://"))
if(GetDevice() == Core.Enums.DeviceType.Android && uri.StartsWith("androidapp://"))
{
launched = _deviceActionService.LaunchApp(uri);
}

View File

@ -8,6 +8,8 @@ namespace Bit.Core.Models.Response
{
public class ErrorResponse
{
public ErrorResponse() { }
public ErrorResponse(JObject response, HttpStatusCode status, bool identityResponse = false)
{
JObject errorModel = null;

View File

@ -37,6 +37,8 @@ namespace Bit.Core.Services
_tokenService = tokenService;
_platformUtilsService = platformUtilsService;
_logoutCallbackAsync = logoutCallbackAsync;
var device = _platformUtilsService.GetDevice();
_deviceType = device.ToString();
}
public bool UrlsSet { get; private set; }
@ -81,7 +83,15 @@ namespace Bit.Core.Services
requestMessage.Headers.Add("Accept", "application/json");
requestMessage.Headers.Add("Device-Type", _deviceType);
var response = await _httpClient.SendAsync(requestMessage);
HttpResponseMessage response;
try
{
response = await _httpClient.SendAsync(requestMessage);
}
catch
{
throw new ApiException(HandleWebError());
}
JObject responseJObject = null;
if(IsJsonResponse(response))
{
@ -321,7 +331,15 @@ namespace Bit.Core.Services
requestMessage.Headers.Add("Accept", "application/json");
}
var response = await _httpClient.SendAsync(requestMessage);
HttpResponseMessage response;
try
{
response = await _httpClient.SendAsync(requestMessage);
}
catch
{
throw new ApiException(HandleWebError());
}
if(hasResponse && response.IsSuccessStatusCode)
{
var responseJsonString = await response.Content.ReadAsStringAsync();
@ -359,7 +377,15 @@ namespace Bit.Core.Services
requestMessage.Headers.Add("Accept", "application/json");
requestMessage.Headers.Add("Device-Type", _deviceType);
var response = await _httpClient.SendAsync(requestMessage);
HttpResponseMessage response;
try
{
response = await _httpClient.SendAsync(requestMessage);
}
catch
{
throw new ApiException(HandleWebError());
}
if(response.IsSuccessStatusCode)
{
var responseJsonString = await response.Content.ReadAsStringAsync();
@ -374,6 +400,15 @@ namespace Bit.Core.Services
}
}
private ErrorResponse HandleWebError()
{
return new ErrorResponse
{
StatusCode = HttpStatusCode.BadGateway,
Message = "There is a problem connecting to the server."
};
}
private async Task<ErrorResponse> HandleErrorAsync(HttpResponseMessage response, bool tokenError)
{
if((tokenError && response.StatusCode == HttpStatusCode.BadRequest) ||
@ -393,8 +428,7 @@ namespace Bit.Core.Services
private bool IsJsonResponse(HttpResponseMessage response)
{
return response.Headers.Contains("content-type") &&
response.Headers.GetValues("content-type").Any(h => h.Contains("application/json"));
return (response.Content?.Headers?.ContentType?.MediaType ?? string.Empty) == "application/json";
}
#endregion

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bit.App.Abstractions;
using Bit.Core.Enums;
using Bit.iOS.Core.Views;
using CoreGraphics;
using Foundation;
@ -16,6 +17,8 @@ namespace Bit.iOS.Services
private Toast _toast;
private UIAlertController _progressAlert;
public DeviceType DeviceType => DeviceType.iOS;
public bool LaunchApp(string appName)
{
throw new NotImplementedException();