Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
"preq",
"psort",
"recid",
"reserialize",
"stringly",
"Szalay",
"unencrypted",
"Xalan",
"Xerces",
"Xunit"
],
"git.enableCommitSigning": true,
Expand Down
92 changes: 68 additions & 24 deletions src/FMData.Rest/FileMakerRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ public override async Task<T> GetByFileMakerIdAsync<T>(
{
return null;
}
throw new Exception(responseObject.Messages.First().Message);
throw new FMDataException(
int.Parse(responseObject.Messages.First().Code),
responseObject.Messages.First().Message,
null);
default:
// other error TODO: Improve handling
throw new Exception($"Find Request Error. Request Uri: {response.RequestMessage.RequestUri} responded with {response.StatusCode}");
Expand Down Expand Up @@ -398,17 +401,32 @@ public override async Task<ICreateResponse> SendAsync<T>(ICreateRequest<T> req)
{
if (string.IsNullOrEmpty(req.Layout)) throw new ArgumentException("Layout is required on the request.");

var responseMessage = await ExecuteRequestAsync(req);
var response = await ExecuteRequestAsync(req);

if (response.StatusCode == HttpStatusCode.InternalServerError)
{
// attempt to read response content
if (response.Content == null) { throw new Exception("Could not read response from Data API."); }

var responseJson = await response.Content.ReadAsStringAsync();
var responseObject = JsonConvert.DeserializeObject<BaseResponse>(responseJson);

// throw FMDataException
throw new FMDataException(
int.Parse(responseObject.Messages.First().Code),
responseObject.Messages.First().Message,
null);
}

try
{
var responseJson = await responseMessage.Content.ReadAsStringAsync();
var responseJson = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<CreateResponse>(responseJson);
}
catch (Exception ex)
{
// something bad happened. TODO: improve non-OK response handling
throw new Exception($"Non-OK Response: Status = {responseMessage.StatusCode}.", ex);
// something bad happened.
throw new Exception($"Non-OK Response: Status = {response.StatusCode}.", ex);
}
}

Expand All @@ -430,6 +448,21 @@ public override async Task<IEditResponse> SendAsync<T>(IEditRequest<T> req)
return new BaseResponse("404", "Error") as EditResponse;
}

if (response.StatusCode == HttpStatusCode.InternalServerError)
{
// attempt to read response content
if (response.Content == null) { throw new Exception("Could not read response from Data API."); }

var responseJson = await response.Content.ReadAsStringAsync();
var responseObject = JsonConvert.DeserializeObject<BaseResponse>(responseJson);

// throw FMDataException
throw new FMDataException(
int.Parse(responseObject.Messages.First().Code),
responseObject.Messages.First().Message,
null);
}

try
{
var responseJson = await response.Content.ReadAsStringAsync();
Expand All @@ -439,7 +472,7 @@ public override async Task<IEditResponse> SendAsync<T>(IEditRequest<T> req)
}
catch (Exception ex)
{
// something bad happened. TODO: improve non-OK response handling
// something bad happened.
throw new Exception($"Non-OK Response: Status = {response.StatusCode}.", ex);
}
}
Expand All @@ -461,6 +494,21 @@ public override async Task<IResponse> SendAsync(IDeleteRequest req)
return new BaseResponse("404", "Error");
}

if (response.StatusCode == HttpStatusCode.InternalServerError)
{
// attempt to read response content
if (response.Content == null) { throw new Exception("Could not read response from Data API."); }

var responseJson = await response.Content.ReadAsStringAsync();
var responseObject = JsonConvert.DeserializeObject<BaseResponse>(responseJson);

// throw FMDataException
throw new FMDataException(
int.Parse(responseObject.Messages.First().Code),
responseObject.Messages.First().Message,
null);
}

try
{
var responseJson = await response.Content.ReadAsStringAsync();
Expand All @@ -469,7 +517,7 @@ public override async Task<IResponse> SendAsync(IDeleteRequest req)
}
catch (Exception ex)
{
// something bad happened. TODO: improve non-OK response handling
// something bad happened.
throw new Exception($"Non-OK Response: Status = {response.StatusCode}.", ex);
}
}
Expand Down Expand Up @@ -540,25 +588,21 @@ public override async Task<IResponse> SendAsync(IDeleteRequest req)

if (response.StatusCode == HttpStatusCode.InternalServerError)
{
try
{
// attempt to read response content
if (response.Content == null) { throw new Exception("Could not read response from Data API."); }

var responseJson = await response.Content.ReadAsStringAsync();
var responseObject = JsonConvert.DeserializeObject<BaseResponse>(responseJson);
if (responseObject.Messages.Any(m => m.Code == "401"))
{
// filemaker no records match the find request => empty list.
return (new List<T>(), new DataInfoModel());
}
// attempt to read response content
if (response.Content == null) { throw new Exception("Could not read response from Data API."); }

throw new Exception(responseObject.Messages.First().Message);
}
catch (Exception ex)
var responseJson = await response.Content.ReadAsStringAsync();
var responseObject = JsonConvert.DeserializeObject<BaseResponse>(responseJson);
if (responseObject.Messages.Any(m => m.Code == "401"))
{
throw new Exception("Could not read response from Data API.", ex);
// filemaker no records match the find request => empty list.
return (new List<T>(), new DataInfoModel());
}
// throw FMDataException for anything not a 401.
throw new FMDataException(
int.Parse(responseObject.Messages.First().Code),
responseObject.Messages.First().Message,
null);
}

// not found, so return empty list
Expand All @@ -567,7 +611,7 @@ public override async Task<IResponse> SendAsync(IDeleteRequest req)
return (new List<T>(), new DataInfoModel());
}

// other error TODO: Improve handling
// other error
throw new Exception($"Find Request Error. Request Uri: {response.RequestMessage.RequestUri} responded with {response.StatusCode}");
}
#endregion
Expand Down
1 change: 1 addition & 0 deletions src/FMData/FMData.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard1.3;net45</TargetFrameworks>
<LangVersion>latest</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyTitle>FMData</AssemblyTitle>
<AssemblyName>FMData</AssemblyName>
Expand Down
Loading