public async Task <bool> HandleAsync(DeleteMenu command) { var menu = await repository.GetByIdAsync(command.MenuId); if (menu == null) { MenuDoesNotExistException.Raise(command, command.MenuId); } // TODO: Check if the user owns the resource before any operation // if(command.User.TenantId != menu.TenantId) // { // throw NotAuthorizedException() // } var successful = await repository.DeleteAsync(command.MenuId); if (!successful) { OperationFailedException.Raise(command, command.MenuId, "Unable to delete menu"); } await applicationEventPublisher.PublishAsync( new MenuDeleted(command, command.MenuId) ); return(successful); }
public async Task GivenAFhirMediator_WhenGettingAnExistingBulkImportTaskThatWasCanceled_ThenOperationFailedExceptionIsThrownWithBadRequestHttpResponseCode() { OperationFailedException ofe = await Assert.ThrowsAsync <OperationFailedException>(() => SetupAndExecuteGetBulkImportTaskByIdAsync(TaskStatus.Queued, true)); Assert.NotNull(ofe); Assert.Equal(_failureStatusCode, ofe.ResponseStatusCode); }
/// <summary> /// 設定 OAuth /// </summary> /// <param name="app"></param> public void ConfigureOAuth(IAppBuilder app) { app.UseOAuthBearerAuthentication(OAuthConfigUtil.OAuthBearerAuthenticationOptions); Internal.Utils.WebApi.Attributes.OAuthAuthorizeAttribute.ValidationFailedAction = (exception, actionContext) => { OperationFailedException operationFailedException = new OperationFailedException(ResultCodeSettingEnum.AuthorizationFailed); ExceptionResponse <ResultCodeSettingEnum> exceptionResponse = operationFailedException.getResponse(); actionContext.Response = actionContext.Request.CreateResponse(System.Net.HttpStatusCode.OK, exceptionResponse); }; }
private static OperationFailedException OperationFailed(string why, string details) { log.ErrorFormat("Operation failed: {0}", why); var ex = new OperationFailedException() { Why = why }; if (details != null) { ex.Details = details; } return(ex); }
public async Task GivenAFhirMediator_WhenGettingAnCompletedImportTaskWithFailure_ThenHttpResponseCodeShouldBeExpected() { ImportTaskErrorResult expectedResult = new ImportTaskErrorResult(); expectedResult.HttpStatusCode = HttpStatusCode.BadRequest; TaskResultData taskResultData = new TaskResultData() { Result = TaskResult.Fail, ResultData = JsonConvert.SerializeObject(expectedResult), }; OperationFailedException ofe = await Assert.ThrowsAsync <OperationFailedException>(() => SetupAndExecuteGetBulkImportTaskByIdAsync(TaskStatus.Completed, false, taskResultData)); Assert.Equal(HttpStatusCode.BadRequest, ofe.ResponseStatusCode); Assert.NotNull(ofe.Message); }
public async Task GivenSetSecretFails_WhenCreatingAnExportJob_ThenThrowsOperationFailedException() { // Set up create export request handler with mock secret store. ISecretStore mockSecretStore = Substitute.For <ISecretStore>(); HttpStatusCode errorStatusCode = HttpStatusCode.InternalServerError; mockSecretStore.SetSecretAsync(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <CancellationToken>()) .Returns <SecretWrapper>(_ => throw new SecretStoreException(SecretStoreErrors.SetSecretError, innerException: null, statusCode: errorStatusCode)); _createExportRequestHandler = new CreateExportRequestHandler(_claimsExtractor, _fhirOperationDataStore, mockSecretStore); var request = new CreateExportRequest(RequestUrl, DestinationType, ConnectionString); OperationFailedException ofe = await Assert.ThrowsAsync <OperationFailedException>(() => _createExportRequestHandler.Handle(request, _cancellationToken)); Assert.NotNull(ofe); Assert.Equal(errorStatusCode, ofe.ResponseStatusCode); }
public async Task GivenAFhirMediator_WhenCancelingExistingBulkImportTaskThatHasAlreadyCompleted_ThenConflictStatusCodeShouldBeReturned(TaskStatus taskStatus) { OperationFailedException operationFailedException = await Assert.ThrowsAsync <OperationFailedException>(async() => await SetupAndExecuteCancelImportAsync(taskStatus, HttpStatusCode.Conflict)); Assert.Equal(HttpStatusCode.Conflict, operationFailedException.ResponseStatusCode); }
private static OperationFailedException OperationFailed(string why, string details) { log.ErrorFormat("Operation failed: {0}", why); var ex = new OperationFailedException() { Why = why }; if (details != null) ex.Details = details; return ex; }
public async Task <TResult> HandleAsync(TCommand command) { TResult result = default(TResult); var menu = await repository.GetByIdAsync(command.MenuId); if (menu == null) { MenuDoesNotExistException.Raise(command, command.MenuId); } // TODO: Check if the user owns the resource before any operation // if(command.User.TenantId != menu.TenantId) // { // throw NotAuthorizedException() // } try { result = await HandleCommandAsync(menu, command); var issuccessful = await repository.SaveAsync(menu); if (!issuccessful) { OperationFailedException.Raise(command, command.MenuId, $"Unable to handle command {typeof(ICommand).Name}"); } foreach (var appEvent in RaiseApplicationEvents(menu, command)) { await applicationEventPublisher.PublishAsync(appEvent); } } catch (DomainExceptionBase ex) { DomainRuleViolationException.Raise(command, command.MenuId, ex); } catch (ApplicationExceptionBase) { // TODO: handle applicaiton exception handling // possible failures is missing data or information, validations, and so on throw; } catch (InfrastructureExceptionBase) { //TODO: handle infrastructure exception handling //possible failures is calling database, queue or any other dependency throw; } catch (Exception ex) { //TODO: Enrich the exception details with context information to track in the logs ex.Data["OperationCode"] = command.OperationCode; ex.Data["CorrelationId"] = command.CorrelationId; ex.Data["MenuId"] = command.MenuId; throw; } return(result); }
internal static SpannerException FromOperationFailedException(OperationFailedException rpcException) => s_apiCodeToErrorCodes.TryGetValue(rpcException?.Status.Code ?? -1, out ErrorCode errorCode) ? new SpannerException(errorCode, rpcException?.Message ?? "An unknown Rpc error occurred.") : new SpannerException(ErrorCode.Unknown, rpcException?.Message ?? "An unknown Rpc error occurred.");
/// <summary> /// 轉換已知的例外(繼承[OperationFailedException]之類別)為API回傳結果 /// </summary> /// <param name="operationFailedException">繼承[OperationFailedException]之例外</param> /// <param name="cultureInfo">與此例外回傳訊息相關的文化特性</param> /// <returns>回傳的例外訊息</returns> public static ExceptionResponse <ResultCodeSettingEnum> getResponse(this OperationFailedException operationFailedException, CultureInfo cultureInfo) { return(new ExceptionResponse <ResultCodeSettingEnum>(operationFailedException, ResponseConfig.ResultCodeFormatter, ResponseConfig.ResultCodeDefinition)); }
/// <summary> /// 使用已知的例外(繼承[OperationFailedException]之類別)為API回傳結果 /// </summary> /// <param name="operationFailedException">繼承[OperationFailedException]之例外</param> /// <returns>回傳的例外訊息</returns> public static ExceptionResponse <ResultCodeSettingEnum> getResponse(this OperationFailedException operationFailedException) { return(operationFailedException.getResponse(operationFailedException.CultureInfo)); }
/// <summary> /// 使用已知例外的UI執行緒的文化特性,格式化已知例外的訊息 /// </summary> /// <param name="operationFailedException">要格式化的例外</param> /// <param name="formatType">格式化方式</param> /// <returns>格式化後的結果</returns> public static string getFormattedString(this OperationFailedException operationFailedException, ResultCodeFormatType formatType) { return(ResponseConfig.ResultCodeFormatter.Format(formatType, operationFailedException.ResultCodeSettingEnum, operationFailedException.CultureInfo)); }