Skip to content

Commit

Permalink
Add proper error-reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSaminator committed Nov 19, 2023
1 parent 4abd879 commit 10158a3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
48 changes: 36 additions & 12 deletions AppMode/AddPlaceToListByAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,33 @@ public AddPlaceToListByAddress(List<string> places) {
private readonly UiInputField _city = new() { Size = new SKRect(400, 466, 784, 514) };
private readonly UiInputField _state = new() { Size = new SKRect(816, 466, 984, 514) };
private readonly UiInputField _postCode = new() { Size = new SKRect(1016, 466, 1200, 514) };
private readonly UiLabel _errorMessage = new() { AnchorPoint = new SKPoint(800, 720), Style = new TextStyle { Color = new SKColor(0xFF, 0x55, 0x55), FontSize = 24 } };

private readonly UiButton _add = new() { Size = new SKRect(500, 600, 784, 648), Text = "Add" };
private readonly UiButton _cancel = new() { Size = new SKRect(816, 600, 1100, 648), Text = "Cancel" };

private readonly UiRoot _ui = new();

private AddPlaceToListByAddress(List<string> places, string houseNum, string streetName, string city, string state, string postCode, string? errorMessage = null) {
_places = places;

_houseNum.Builder.Append(houseNum);
_streetName.Builder.Append(streetName);
_city.Builder.Append(city);
_state.Builder.Append(state);
_postCode.Builder.Append(postCode);
if (errorMessage != null)
_errorMessage.Text = errorMessage;
}

private static async Task<List<string>?> PerformAddressQuery(string houseNum, string streetName, string city, string state, string postCode) {
var taskResult = await XmlHelper.ConstructOsmQuery(
(scriptDoc, osmScript) => scriptDoc.AddressQuery(osmScript, houseNum, streetName, city, state, postCode)
).GetPlaceXml();

var osmRoot = taskResult["osm"];
if (osmRoot == null) {
Console.WriteLine($"Got error document: {taskResult.ToXmlString()}");
await Console.Error.WriteLineAsync($"Got error document from address query: {taskResult.ToXmlString()}");
return null;
}

Expand All @@ -46,27 +59,37 @@ public AddPlaceToListByAddress(List<string> places) {
where wayNode is XmlElement
select "id:" + ((XmlElement)wayNode).GetAttribute("id");

return places.ToList();
var placesList = places.ToList();

return placesList.Any() ? placesList : null;
}

private void AddFromAddress() {
_add.IsEnabled = false;
_cancel.IsEnabled = false;

var houseNum = _houseNum.Builder.ToString();
var streetName = _streetName.Builder.ToString();
var city = _city.Builder.ToString();
var state = _state.Builder.ToString();
var postCode = _postCode.Builder.ToString();

PerformAddressQuery(
_houseNum.Builder.ToString(),
_streetName.Builder.ToString(),
_city.Builder.ToString(),
_state.Builder.ToString(),
_postCode.Builder.ToString()
houseNum,
streetName,
city,
state,
postCode
).ContinueWith(task => {
var taskResult = task.Result;
FmsApp.Instance.PostAction(() => {
var places = _places.ToList();
if (taskResult != null)
if (taskResult == null)
FmsApp.Instance.PostAction(() => { _next = new AddPlaceToListByAddress(_places.ToList(), houseNum, streetName, city, state, postCode, "Unable to find location on OpenStreetMap with that address"); });
else
FmsApp.Instance.PostAction(() => {
var places = _places.ToList();
places.AddRange(taskResult);
_next = new InitialPlaceListBuilding(places);
});
_next = new InitialPlaceListBuilding(places);
});
});
}

Expand All @@ -83,6 +106,7 @@ public override void Initialize() {
_ui.AddChild(_city);
_ui.AddChild(_state);
_ui.AddChild(_postCode);
_ui.AddChild(_errorMessage);
_ui.AddChild(_add);
_ui.AddChild(_cancel);
}
Expand Down
16 changes: 8 additions & 8 deletions AppMode/LoadingOsmData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private static async Task ProcessRequestedWays(List<Task<XmlDocument>> taskQueue

var osmRoot = taskResult["osm"];
if (osmRoot == null) {
Console.WriteLine($"Got error document: {taskResult.ToXmlString()}");
await Console.Error.WriteLineAsync($"Got error document: {taskResult.ToXmlString()}");
continue;
}

Expand All @@ -45,7 +45,7 @@ private static async Task ProcessRequestedWays(List<Task<XmlDocument>> taskQueue
var metaElement = osmRoot["meta"];
if (metaElement != null) osmRoot.RemoveChild(metaElement);

Console.WriteLine($"Processing result xml: {taskResult.ToXmlString()}");
await Console.Out.WriteLineAsync($"Processing result xml: {taskResult.ToXmlString()}");

// Disambiguate type of response: way
foreach (var wayObj in osmRoot.GetElementsByTagName("way")) {
Expand Down Expand Up @@ -80,8 +80,8 @@ private static async Task ProcessRequestedWays(List<Task<XmlDocument>> taskQueue

var nodeId = nodeElement.GetAttribute("id");
var node = OsmNode.GetNode(nodeId);
if (!double.TryParse(nodeElement.GetAttribute("lat"), out node.Location.Y)) Console.WriteLine($"Unable to get latitude from node with id {nodeId}");
if (!double.TryParse(nodeElement.GetAttribute("lon"), out node.Location.X)) Console.WriteLine($"Unable to get longitude from node with id {nodeId}");
if (!double.TryParse(nodeElement.GetAttribute("lat"), out node.Location.Y)) await Console.Error.WriteLineAsync($"Unable to get latitude from node with id {nodeId}");
if (!double.TryParse(nodeElement.GetAttribute("lon"), out node.Location.X)) await Console.Error.WriteLineAsync($"Unable to get longitude from node with id {nodeId}");

if (!node.Location.IsNowhere) {
if (mode == QueryMode.BBoxExpansion)
Expand Down Expand Up @@ -124,7 +124,7 @@ private static async Task ProcessRequestedWays(List<Task<XmlDocument>> taskQueue
var result = new OsmQueryResult();
result.BBox.Clear();

Console.WriteLine("Phase 1 of loading...");
await Console.Out.WriteLineAsync("Phase 1 of loading...");
await ProcessRequestedWays(taskQueue, QueryMode.BBoxExpansion, result);

if (result.BBox.IsEmpty)
Expand All @@ -135,7 +135,7 @@ private static async Task ProcessRequestedWays(List<Task<XmlDocument>> taskQueue
(scriptDoc, osmScript) => scriptDoc.BBoxQuery(osmScript, result.BBox, wayTags)
).GetPlaceXml());

Console.WriteLine("Phase 2 of loading...");
await Console.Out.WriteLineAsync("Phase 2 of loading...");
await ProcessRequestedWays(taskQueue, QueryMode.PopulateLists, result);

return result;
Expand All @@ -149,7 +149,7 @@ private static async Task<List<OsmWay>> PerformPlacesQuery(IEnumerable<string> p
(scriptDoc, osmScript) => scriptDoc.NameQuery(osmScript, place)
).GetPlaceXml()).ToList();

Console.WriteLine("Phase 2 of loading...");
await Console.Out.WriteLineAsync("Phase 2 of loading...");
await ProcessRequestedWays(taskQueue, QueryMode.PopulateLists, result);

return result.Ways;
Expand All @@ -167,7 +167,7 @@ private static async Task<OsmQueryResult> PerformBBoxQuery(MapBoundingBox bbox)
).GetPlaceXml()
};

Console.WriteLine("Phase 2 of loading...");
await Console.Out.WriteLineAsync("Phase 2 of loading...");
await ProcessRequestedWays(taskQueue, QueryMode.PopulateLists, result);

return result;
Expand Down
2 changes: 1 addition & 1 deletion FancyMapSnapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<IsPackable>false</IsPackable>
<Company>Sam Francis</Company>
<Product>Fancy Map Snapper</Product>
<AssemblyVersion>1.1.1</AssemblyVersion>
<AssemblyVersion>1.1.2</AssemblyVersion>
<NeutralLanguage>en-US</NeutralLanguage>
</PropertyGroup>

Expand Down

0 comments on commit 10158a3

Please sign in to comment.