Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt the exception unification changes #3194

Merged
merged 12 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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: 2 additions & 2 deletions .github/templates/test-ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ jobs:
- name: Set XCode Version
shell: bash
run: |
sudo xcode-select -s "/Applications/Xcode_14.1.app"
echo "MD_APPLE_SDK_ROOT=/Applications/Xcode_14.1.app" >> $GITHUB_ENV
sudo xcode-select -s "/Applications/Xcode_14.2.app"
echo "MD_APPLE_SDK_ROOT=/Applications/Xcode_14.2.app" >> $GITHUB_ENV
- name: Setup workloads
run: |
dotnet workload install maui
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ jobs:
- name: Set XCode Version
shell: bash
run: |
sudo xcode-select -s "/Applications/Xcode_14.1.app"
echo "MD_APPLE_SDK_ROOT=/Applications/Xcode_14.1.app" >> $GITHUB_ENV
sudo xcode-select -s "/Applications/Xcode_14.2.app"
echo "MD_APPLE_SDK_ROOT=/Applications/Xcode_14.2.app" >> $GITHUB_ENV
- name: Setup workloads
run: |
dotnet workload install maui
Expand Down
4 changes: 2 additions & 2 deletions Realm/Realm/Exceptions/RealmClassLacksPrimaryKeyException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class RealmClassLacksPrimaryKeyException : RealmException
{
internal RealmClassLacksPrimaryKeyException(string message) : base(message)
{
HelpLink = "https://docs.mongodb.com/realm/dotnet/objects/#primary-key";
HelpLink = "https://www.mongodb.com/docs/realm/sdk/dotnet/model-data/define-object-model/#primary-key";

Check warning

Code scanning / CodeQL

Virtual call in constructor or destructor

Avoid virtual calls in a constructor or destructor.
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class RealmDuplicatePrimaryKeyValueException : RealmException
{
internal RealmDuplicatePrimaryKeyValueException(string message) : base(message)
{
HelpLink = "https://docs.mongodb.com/realm/dotnet/objects/#primary-key";
HelpLink = "https://www.mongodb.com/docs/realm/sdk/dotnet/model-data/define-object-model/#primary-key";

Check warning

Code scanning / CodeQL

Virtual call in constructor or destructor

Avoid virtual calls in a constructor or destructor.
}
}
}
96 changes: 37 additions & 59 deletions Realm/Realm/Exceptions/RealmException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using Realms.Sync.Exceptions;

namespace Realms.Exceptions
Expand All @@ -27,16 +26,6 @@ namespace Realms.Exceptions
/// </summary>
public class RealmException : Exception
{
private static readonly IDictionary<RealmExceptionCodes, Func<string, string, Exception>> _overriders = new Dictionary<RealmExceptionCodes, Func<string, string, Exception>>();

internal static void AddOverrider(RealmExceptionCodes code, Func<string, string, Exception> handler)
{
if (!_overriders.ContainsKey(code))
{
_overriders.Add(code, handler);
}
}

internal RealmException(string detailMessage) : base(detailMessage)
{
}
Expand All @@ -45,101 +34,90 @@ internal RealmException(string detailMessage, Exception innerException) : base(d
{
}

internal static Exception Create(RealmExceptionCodes exceptionCode, string message, string detail)
internal static Exception Create(RealmExceptionCodes exceptionCode, string message, RealmExceptionCategories categories)
{
if (_overriders.TryGetValue(exceptionCode, out var handler))
{
return handler(message, detail);
}

// these are increasing enum value order
switch (exceptionCode)
{
case RealmExceptionCodes.RealmError:
return new RealmException(message);

case RealmExceptionCodes.RealmFileAccessError:
return new RealmFileAccessErrorException(message);

case RealmExceptionCodes.RealmDecryptionFailed:
case RealmExceptionCodes.RLM_ERR_DECRYPTION_FAILED:
return new RealmDecryptionFailedException(message);

case RealmExceptionCodes.RealmFileExists:
case RealmExceptionCodes.RLM_ERR_FILE_ALREADY_EXISTS:
return new RealmFileExistsException(message);

case RealmExceptionCodes.RealmFileNotFound:
case RealmExceptionCodes.RLM_ERR_FILE_NOT_FOUND:
return new RealmFileNotFoundException(message);

case RealmExceptionCodes.RealmInvalidDatabase:
case RealmExceptionCodes.RLM_ERR_INVALID_DATABASE:
return new RealmInvalidDatabaseException(message);

case RealmExceptionCodes.RealmOutOfMemory:
case RealmExceptionCodes.RLM_ERR_OUT_OF_MEMORY:
return new RealmOutOfMemoryException(message);

case RealmExceptionCodes.RealmPermissionDenied:
case RealmExceptionCodes.RLM_ERR_FILE_PERMISSION_DENIED:
return new RealmPermissionDeniedException(message);

case RealmExceptionCodes.RealmMismatchedConfig:
case RealmExceptionCodes.RLM_ERR_MISMATCHED_CONFIG:
return new RealmMismatchedConfigException(message);

case RealmExceptionCodes.RealmInvalidTransaction:
case RealmExceptionCodes.RLM_ERR_WRONG_TRANSACTION_STATE:
return new RealmInvalidTransactionException(message);

case RealmExceptionCodes.RealmFormatUpgradeRequired:
case RealmExceptionCodes.RealmSchemaMismatch:
case RealmExceptionCodes.RLM_ERR_FILE_FORMAT_UPGRADE_REQUIRED:
case RealmExceptionCodes.RLM_ERR_SCHEMA_MISMATCH:
return new RealmMigrationNeededException(message);

case RealmExceptionCodes.RealmRowDetached:
case RealmExceptionCodes.RowDetached:
return new RealmInvalidObjectException(message);

case RealmExceptionCodes.RealmTableHasNoPrimaryKey:
case RealmExceptionCodes.RLM_ERR_MISSING_PRIMARY_KEY:
return new RealmClassLacksPrimaryKeyException(message);

case RealmExceptionCodes.RealmDuplicatePrimaryKeyValue:
case RealmExceptionCodes.DuplicatePrimaryKey:
return new RealmDuplicatePrimaryKeyValueException(message);

case RealmExceptionCodes.RealmClosed:
return new RealmClosedException(message);

case RealmExceptionCodes.RealmSchemaValidation:
case RealmExceptionCodes.RLM_ERR_SCHEMA_VALIDATION_FAILED:
return new RealmSchemaValidationException(message);

case RealmExceptionCodes.NotNullableProperty:
case RealmExceptionCodes.PropertyMismatch:
case RealmExceptionCodes.PropertyTypeMismatch:
return new RealmException(message);

case RealmExceptionCodes.AppClientError:
case RealmExceptionCodes.AppCustomError:
case RealmExceptionCodes.AppHttpError:
case RealmExceptionCodes.AppJsonError:
case RealmExceptionCodes.AppServiceError:
case RealmExceptionCodes.AppUnknownError:
return new AppException(message, helpLink: null, httpStatusCode: 0);

case RealmExceptionCodes.StdArgumentOutOfRange:
case RealmExceptionCodes.StdIndexOutOfRange:
case RealmExceptionCodes.IndexOutOfRange:
return new ArgumentOutOfRangeException(message);

case RealmExceptionCodes.StdInvalidOperation:
return new InvalidOperationException(message);

case RealmExceptionCodes.ObjectManagedByAnotherRealm:
return new RealmObjectManagedByAnotherRealmException(message);

case RealmExceptionCodes.KeyAlreadyExists:
case RealmExceptionCodes.DuplicateSubscription:
return new ArgumentException(message);

case RealmExceptionCodes.SessionError:
var code = int.TryParse(detail, out var intCode) ? (ErrorCode)intCode : ErrorCode.Unknown;

return new SessionException(message, code);
case RealmExceptionCodes.RealmInUseException:
case RealmExceptionCodes.RLM_ERR_DELETE_OPENED_REALM:
return new RealmInUseException(message);

default:
return new Exception(message);
case RealmExceptionCodes.RLM_ERR_MIGRATION_FAILED:
return new RealmMigrationException(message);
}

if (categories.HasFlag(RealmExceptionCategories.RLM_ERR_CAT_APP_ERROR))
{
return new AppException(message, helpLink: null, httpStatusCode: 0);
}

if (categories.HasFlag(RealmExceptionCategories.RLM_ERR_CAT_FILE_ACCESS))
{
return new RealmFileAccessErrorException(message);
}

if (categories.HasFlag(RealmExceptionCategories.RLM_ERR_CAT_INVALID_ARG))
{
return new ArgumentException(message);
}

return new RealmException(message);
}
}
}
39 changes: 39 additions & 0 deletions Realm/Realm/Exceptions/RealmExceptionCategories.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2023 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

using System;

namespace Realms.Exceptions
{
[Flags]
internal enum RealmExceptionCategories
{
RLM_ERR_CAT_LOGIC = 0x0002,
RLM_ERR_CAT_RUNTIME = 0x0004,
RLM_ERR_CAT_INVALID_ARG = 0x0008,
RLM_ERR_CAT_FILE_ACCESS = 0x0010,
RLM_ERR_CAT_SYSTEM_ERROR = 0x0020,
RLM_ERR_CAT_APP_ERROR = 0x0040,
RLM_ERR_CAT_CLIENT_ERROR = 0x0080,
RLM_ERR_CAT_JSON_ERROR = 0x0100,
RLM_ERR_CAT_SERVICE_ERROR = 0x0200,
RLM_ERR_CAT_HTTP_ERROR = 0x0400,
RLM_ERR_CAT_CUSTOM_ERROR = 0x0800,
RLM_ERR_CAT_WEBSOCKET_ERROR = 0x1000,
}
}
Loading