コード例 #1
0
ファイル: LoggingBehaviour.cs プロジェクト: sheaft-app/api
        public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken,
                                             RequestHandlerDelegate <TResponse> next)
        {
            if (request.RequestUser == null)
            {
                throw SheaftException.Unexpected("The requestUser must be assigned for the command.");
            }

            var requestName = typeof(TRequest).Name;

            _logger.LogInformation("Processing request: {Name} for {@UserId}", requestName, request.RequestUser.Name);

            using (var scope = _logger.BeginScope(new Dictionary <string, object>
            {
                ["RequestId"] = request.RequestUser.RequestId,
                ["UserIdentifier"] = request.RequestUser.Id.ToString("N"),
                ["UserEmail"] = request.RequestUser.Email,
                ["UserName"] = request.RequestUser.Name,
                ["Roles"] = string.Join(';', request.RequestUser.Roles),
                ["IsAuthenticated"] = request.RequestUser.IsAuthenticated().ToString(),
                ["Command"] = requestName,
                ["Data"] = JsonConvert.SerializeObject(request),
            }))
            {
                return(await next());
            }
        }
コード例 #2
0
        public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken,
                                             RequestHandlerDelegate <TResponse> next)
        {
            var type = typeof(TRequest).Name;

            try
            {
                return(await next());
            }
            catch (SheaftException sheaftException)
            {
                _logger.LogError(sheaftException, $"Sheaft error on executing {type} : {sheaftException.Message}");
                throw;
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrency)
            {
                _logger.LogError(dbUpdateConcurrency,
                                 $"DbConcurrency error on executing {type} : {dbUpdateConcurrency.Message}");

                throw SheaftException.Conflict(dbUpdateConcurrency);
            }
            catch (DbUpdateException dbUpdate)
            {
                _logger.LogError(dbUpdate, $"DbUpdate error on executing {type} : {dbUpdate.Message}");

                if (dbUpdate.InnerException != null &&
                    dbUpdate.InnerException.Message.Contains("duplicate key row"))
                {
                    throw SheaftException.AlreadyExists(dbUpdate);
                }

                throw SheaftException.BadRequest(dbUpdate);
            }
            catch (NotSupportedException notSupported)
            {
                _logger.LogError(notSupported, $"Not supported error on executing {type} : {notSupported.Message}");
                throw SheaftException.Unexpected(notSupported);
            }
            catch (InvalidOperationException invalidOperation)
            {
                if (invalidOperation.Source == "Microsoft.EntityFrameworkCore" &&
                    invalidOperation.Message.StartsWith("Enumerator failed to MoveNextAsync"))
                {
                    _logger.LogWarning(invalidOperation, $"Entity not found while processing {type}");
                    throw SheaftException.NotFound(invalidOperation);
                }

                _logger.LogError(invalidOperation,
                                 $"Invalid operation error on executing {type} : {invalidOperation.Message}");

                throw SheaftException.Unexpected(invalidOperation);
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Unexpected error on executing {type} : {e.Message}");
                throw SheaftException.Unexpected(e);
            }
        }
コード例 #3
0
ファイル: DocumentsController.cs プロジェクト: sheaft-app/api
        public async Task <byte[]> DownloadDocumentAsync(Guid documentId, RequestUser currentUser, CancellationToken token)
        {
            var legal = await _context.Legals
                        .SingleOrDefaultAsync(l => l.Documents.Any(d => d.Id == documentId), token);

            var document = legal.Documents.FirstOrDefault(d => d.Id == documentId);

            if (document == null)
            {
                return(null);
            }

            byte[] archiveFile;
            using (var archiveStream = new MemoryStream())
            {
                using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create, true))
                {
                    foreach (var page in document.Pages)
                    {
                        var result = await _blobService.DownloadDocumentPageAsync(document.Id, page.Id, legal.User.Id, token);

                        if (!result.Succeeded)
                        {
                            throw SheaftException.Unexpected(result.Exception, result.Message, result.Params);
                        }

                        var zipArchiveEntry = archive.CreateEntry(page.Filename + page.Extension, CompressionLevel.Optimal);
                        using (var zipStream = zipArchiveEntry.Open())
                            await zipStream.WriteAsync(result.Data, 0, result.Data.Length, token);
                    }
                }

                archiveFile = archiveStream.ToArray();
            }

            return(archiveFile);
        }