Skip to content

Commit 2794bdf

Browse files
author
Caitlin Bales (MSFT)
authored
Merge pull request #99 from microsoftgraph/error-handling
Error handling & logging
2 parents 00a0747 + 77e7582 commit 2794bdf

File tree

18 files changed

+3149
-21
lines changed

18 files changed

+3149
-21
lines changed

Templates/Android/generated/BaseEntityCollectionReferenceRequest.java.tt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323
public void post(final <#=TypeName(c)#> new<#=TypeName(c)#>, final ICallback<<#=TypeName(c)#>> callback) {
2424
<#
25-
String prop = c.AsOdcmProperty().GetServiceCollectionNavigationPropertyForPropertyType().Name;
25+
var navigationProperty = c.AsOdcmProperty().GetServiceCollectionNavigationPropertyForPropertyType();
26+
if (navigationProperty != null) {
27+
String prop = c.AsOdcmProperty().GetServiceCollectionNavigationPropertyForPropertyType().Name;
2628
#>
2729
final String requestUrl = getBaseRequest().getRequestUrl().toString();
2830
final ReferenceRequestBody body = new ReferenceRequestBody(getBaseRequest().getClient().getServiceRoot() + "/<#=prop#>/" + new<#=TypeName(c)#>.id);
@@ -38,7 +40,8 @@
3840
.buildRequest(getBaseRequest().getOptions())
3941
.post(new<#=TypeName(c)#>, body);
4042
}
41-
43+
<# }
44+
#>
4245
<# if (c.GetFeatures().CanExpand) { #>
4346
/**
4447
* Sets the expand clause for the request

Templates/CSharp/Base/CollectionRequest.Base.template.tt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ public string GetPostAsyncMethodForReferencesRequest(OdcmProperty odcmProperty)
135135
var templateWriter = (CodeWriterCSharp)templateWriterHost.CodeWriter;
136136

137137
var serviceNavigationProperty = odcmProperty.GetServiceCollectionNavigationPropertyForPropertyType();
138+
if (serviceNavigationProperty == null)
139+
return string.Empty;
138140

139141
var stringBuilder = new StringBuilder();
140142

Templates/ObjC/Requests/EntityCollectionRequest.m.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ if (innerEntityType.Equals(propName))
8989
- (NSMutableURLRequest *)add<#=innerEntity.Name.ToUpperFirstChar()#>:(<#=innerEntityType#>*)<#=innerEntity.Name.ToLowerFirstChar()#>
9090
{
9191
<#
92-
if(collectionReference)
92+
if(collectionReference && prop.GetServiceCollectionNavigationPropertyForPropertyType() != null)
9393
{
9494
#>
9595

src/GraphODataTemplateWriter/.config/TemplateWriterSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"AvailableLanguages": [ "Android", "ObjC", "CSharp", "PHP", "Python", "TypeScript", "GraphEndpointList" ],
33
"TargetLanguage": "CSharp",
44
"Plugins": [ ],

src/GraphODataTemplateWriter/CodeHelpers/Android/TypeHelperAndroid.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ namespace Microsoft.Graph.ODataTemplateWriter.CodeHelpers.Android
66
using Microsoft.Graph.ODataTemplateWriter.Extensions;
77
using Vipr.Core.CodeModel;
88
using System;
9+
using NLog;
910

1011
public static class TypeHelperAndroid
1112
{
13+
private static Logger logger = LogManager.GetCurrentClassLogger();
1214
public const string ReservedPrefix = "msgraph_";
1315
public static HashSet<string> ReservedNames
1416
{
@@ -95,6 +97,7 @@ public static string SanitizePropertyName(this string property, OdcmObject odcmP
9597
{
9698
if (ReservedNames.Contains(property))
9799
{
100+
logger.Info("Property \"{0}\" is a reserved word in Android. Converting to \"{1}{0}\"", property, ReservedPrefix);
98101
return ReservedPrefix + property;
99102
}
100103

@@ -104,6 +107,7 @@ public static string SanitizePropertyName(this string property, OdcmObject odcmP
104107
if (odcmProperty.Projection.Type.Name.ToUpperFirstChar() == odcmProperty.Name.ToUpperFirstChar())
105108
{
106109
// Name the property: {metadataName} + "Property"
110+
logger.Info("Property type \"{0}\" has the same name as the class. Converting to \"{0}Property\"", property);
107111
return string.Concat(property, "Property");
108112
}
109113

src/GraphODataTemplateWriter/CodeHelpers/CSharp/TypeHelperCSharp.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ namespace Microsoft.Graph.ODataTemplateWriter.CodeHelpers.CSharp
66
using System.Collections.Generic;
77
using Microsoft.Graph.ODataTemplateWriter.Extensions;
88
using Vipr.Core.CodeModel;
9+
using NLog;
910

1011
public static class TypeHelperCSharp
1112
{
13+
private static Logger logger = LogManager.GetCurrentClassLogger();
14+
1215
public const string DefaultReservedPrefix = "@";
1316
public static ICollection<string> GetReservedNames()
1417
{
@@ -235,6 +238,7 @@ public static string GetSanitizedPropertyName(this string property, OdcmProperty
235238
{
236239
var reservedPrefix = string.IsNullOrEmpty(prefix) ? DefaultReservedPrefix : prefix;
237240

241+
logger.Info("Property \"{0}\" is a reserved word in .NET. Converting to \"{1}{0}\"", property.ToUpperFirstChar(), reservedPrefix);
238242
return string.Concat(reservedPrefix, property.ToUpperFirstChar());
239243
}
240244

@@ -247,6 +251,7 @@ public static string GetSanitizedPropertyName(this string property, OdcmProperty
247251
if (odcmProperty.Projection.Type.Name.ToUpperFirstChar() == odcmProperty.Class.Name.ToUpperFirstChar())
248252
{
249253
// Name the property: {metadataName} + "Property"
254+
logger.Info("Property type \"{0}\" has the same name as the class. Converting to \"{0}Property\"", property);
250255
return string.Concat(property, "Property");
251256
}
252257

src/GraphODataTemplateWriter/CodeHelpers/ObjC/TypeHelperObjC.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ namespace Microsoft.Graph.ODataTemplateWriter.CodeHelpers.ObjC
77
using Microsoft.Graph.ODataTemplateWriter.Extensions;
88
using Microsoft.Graph.ODataTemplateWriter.Settings;
99
using Vipr.Core.CodeModel;
10+
using NLog;
1011

1112
public static class TypeHelperObjC
1213
{
1314
public static string Prefix = ConfigurationService.Settings.NamespacePrefix;
1415

16+
private static Logger logger = LogManager.GetCurrentClassLogger();
17+
1518
private static ICollection<string> reservedNames;
1619
public static ICollection<string> ReservedNames
1720
{
@@ -157,6 +160,10 @@ public static string SanitizePropertyName(this OdcmProperty property)
157160
{
158161
if (ReservedNames.Contains(property.Name.ToLower()))
159162
{
163+
logger.Info("Property \"{0}\" is a reserved word in Objective-C. Converting to \"{1}{0}\"",
164+
property.Name.ToUpperFirstChar(),
165+
property.Class.Name.ToLowerFirstChar()
166+
);
160167
return property.Class.Name.ToLowerFirstChar() + property.Name.ToUpperFirstChar();
161168
}
162169
return property.Name;

src/GraphODataTemplateWriter/CodeHelpers/PHP/TypeHelperPHP.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ namespace Microsoft.Graph.ODataTemplateWriter.CodeHelpers.PHP
66
using Microsoft.Graph.ODataTemplateWriter.Extensions;
77
using Vipr.Core.CodeModel;
88
using System;
9+
using NLog;
910

1011
public static class TypeHelperPHP
1112
{
1213
public const string ReservedPrefix = "Graph";
14+
private static Logger logger = LogManager.GetCurrentClassLogger();
15+
1316
public static HashSet<string> ReservedNames
1417
{
1518
get
@@ -120,6 +123,7 @@ public static string SanitizeEntityName(this String name)
120123
{
121124
if (ReservedNames.Contains(name))
122125
{
126+
logger.Info("Property \"{0}\" is a reserved word in PHP. Converting to \"{1}{0}\"", name, ReservedPrefix);
123127
return ReservedPrefix + name.ToCheckedCase();
124128
}
125129

@@ -130,6 +134,7 @@ public static string SanitizePropertyName(this String name, String entityName)
130134
{
131135
if (name == "properties")
132136
{
137+
logger.Info("Property \"{0}\" is a reserved word in PHP. Converting to \"{1}{0}\"", name, ReservedPrefix);
133138
return entityName + name.ToCheckedCase();
134139
}
135140

src/GraphODataTemplateWriter/CodeHelpers/Python/TypeHelperPython.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ namespace Microsoft.Graph.ODataTemplateWriter.CodeHelpers.Python
55
using System.Collections.Generic;
66
using Microsoft.Graph.ODataTemplateWriter.Extensions;
77
using Vipr.Core.CodeModel;
8+
using NLog;
89

910
public static class TypeHelperPython
1011
{
1112
public const string ReservedPrefix = "$$__$$";
13+
private static Logger logger = LogManager.GetCurrentClassLogger();
14+
1215
public static ICollection<string> ReservedNames
1316
{
1417
get
@@ -102,6 +105,7 @@ public static string GetToLowerFirstCharName(this OdcmProperty property)
102105
public static string SanitizePropertyName(this OdcmProperty property)
103106
{
104107
if (ReservedNames.Contains(property.Name.ToLower())) {
108+
logger.Info("Property \"{0}\" is a reserved word in Python. Converting to \"{1}{0}\"", property.Name, ReservedPrefix);
105109
return ReservedPrefix + property.Name;
106110
}
107111
return property.Name;

src/GraphODataTemplateWriter/Extensions/OdcmModelExtensions.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ namespace Microsoft.Graph.ODataTemplateWriter.Extensions
77
using System.Linq;
88
using Microsoft.Graph.ODataTemplateWriter.Settings;
99
using Vipr.Core.CodeModel;
10+
using NLog;
1011

1112
public static class OdcmModelExtensions
1213
{
14+
private static Logger logger = LogManager.GetCurrentClassLogger();
15+
1316
public static bool IsCollection(this OdcmProperty odcmProperty)
1417
{
1518
return odcmProperty.IsCollection;
@@ -140,14 +143,22 @@ public static OdcmProperty GetServiceCollectionNavigationPropertyForPropertyType
140143
// class object. Use First() instead of FirstOrDefault() so template generation would fail if not found
141144
// instead of silently continuing. If an entity is used in a reference property a navigation collection
142145
// on the client for that type is required.
143-
return odcmProperty
144-
.Class
145-
.Namespace
146-
.Classes
147-
.Where(odcmClass => odcmClass.Kind == OdcmClassKind.Service)
148-
.SelectMany(service => (service as OdcmServiceClass).NavigationProperties())
149-
.Where(property => property.IsCollection && property.Projection.Type.FullName.Equals(odcmProperty.Projection.Type.FullName))
150-
.First();
146+
try
147+
{
148+
return odcmProperty
149+
.Class
150+
.Namespace
151+
.Classes
152+
.Where(odcmClass => odcmClass.Kind == OdcmClassKind.Service)
153+
.SelectMany(service => (service as OdcmServiceClass).NavigationProperties())
154+
.Where(property => property.IsCollection && property.Projection.Type.FullName.Equals(odcmProperty.Projection.Type.FullName))
155+
.First();
156+
} catch (Exception e)
157+
{
158+
logger.Error("The navigation property \"{0}\" on class \"{1}\" does not specify it is self-contained nor is it defined in an EntitySet", odcmProperty.Name.ToString(), odcmProperty.Class.FullName.ToString());
159+
logger.Error(e);
160+
return null;
161+
}
151162
}
152163

153164
public static IEnumerable<OdcmProperty> NavigationProperties(this OdcmClass odcmClass)

0 commit comments

Comments
 (0)