Skip to content

Commit c97dcb3

Browse files
committed
Handle potential null return codes in GetReturnCode()
The `GetReturnCode()` method assumes that the operation it is applied to will return a `@ReturnCode` (or a parameter corresponding to the `sqlParameter` parameter). If the operation doesn't return one, an unintuitive and unexpected exception is thrown. This is mitigated in this commit by instead falling back to a generic error code of `-1`. This addresses, for instance, a bug where the `GetTopicId()` function wasn't returning a value if the `@TopicKey` parameter couldn't be found. That bug has since been fixed (d517971)—but we should be setup to handle that potentiality regardless. This also provides flexibility for databases that haven't updated `GetTopicId()` yet.
1 parent d517971 commit c97dcb3

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

OnTopic.Data.Sql/SqlCommandExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ internal static int GetReturnCode(this SqlCommand command, string sqlParameter =
3333
$"The call to the {command.CommandText} stored procedure did not return the expected 'ReturnCode' parameter."
3434
);
3535
var returnCode = command.Parameters[$"@{sqlParameter}"].Value.ToString();
36-
return Int32.Parse(returnCode, CultureInfo.InvariantCulture);
36+
if (Int32.TryParse(returnCode, NumberStyles.Integer, CultureInfo.InvariantCulture, out var returnValue)) {
37+
return returnValue;
38+
}
39+
return -1;
3740
}
3841

3942
/*==========================================================================================================================

0 commit comments

Comments
 (0)