umbraco / Umbraco-CMS

Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
https://umbraco.com
MIT License
4.53k stars 2.7k forks source link

System.InvalidOperationException: Failed to Fetch Ancestors - Internal Server Error (500) Due to Content Cache Retrieval Failure in Umbraco Delivery API #17165

Open acbach opened 1 month ago

acbach commented 1 month ago

Which Umbraco version are you using? (Please write the exact version, example: 10.1.0)

13.5.1

Bug summary

When making a GET request to the Umbraco Delivery API (v2) to fetch ancestor content using the fetch=ancestors parameter for a specific content ID that is unpublished, the server returns a 500 Internal Server Error. The error is triggered by a System.InvalidOperationException, indicating that the content cache could not be obtained due to the unpublished state of the target content. This issue arises during the execution of the ApiContentQueryService while processing the query with the AncestorsSelector.

Additional Notes: The content ID being queried is unpublished, which may contribute to the failure of obtaining the content cache.

Stack Trace Highlights: System.InvalidOperationException: Could not obtain the content cache Umbraco.Cms.Api.Delivery.Querying.Selectors.AncestorsSelector.BuildSelectorOption(String selector) Umbraco.Cms.Api.Delivery.Services.ApiContentQueryService.ExecuteQuery

Environment: Umbraco CMS Delivery API v2 Localhost environment Unpublished content ID

Specifics

curl -X 'GET' \ 'https://localhost:44369/umbraco/delivery/api/v2/content?fetch=ancestors%3A6fa39e0b-c9f0-4238-a1f3-dc7eaf846d63&skip=0&take=10&fields=properties%5B%24all%5D' \ -H 'accept: application/json' \ -H 'Api-Key: set-in-umbraco-cloud-environment-secrets' \ -H 'Preview: true'

Returns status code 500 internal server error

Response payload { "type": "https://tools.ietf.org/html/rfc9110#section-15.6.1", "title": "System.InvalidOperationException", "status": 500, "detail": "Could not obtain the content cache", "traceId": "00-f5cac7e00e54c75e9453099063522718-2dd8abfcbaede610-00", "exception": { "details": "System.InvalidOperationException: Could not obtain the content cache\r\n at Umbraco.Cms.Api.Delivery.Querying.Selectors.AncestorsSelector.BuildSelectorOption(String selector)\r\n at Umbraco.Cms.Api.Delivery.Services.ApiContentQueryService.GetSelectorOption(String fetch)\r\n at Umbraco.Cms.Api.Delivery.Services.ApiContentQueryService.ExecuteQuery(String fetch, IEnumerable1 filters, IEnumerable1 sorts, ProtectedAccess protectedAccess, Int32 skip, Int32 take)\r\n at Umbraco.Cms.Api.Delivery.Controllers.Content.QueryContentApiController.HandleRequest(String fetch, String[] filter, String[] sort, Int32 skip, Int32 take)\r\n at Umbraco.Cms.Api.Delivery.Controllers.Content.QueryContentApiController.QueryV20(String fetch, String[] filter, String[] sort, Int32 skip, Int32 take)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.gLogged|12_1(ControllerActionInvoker invoker)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()\r\n--- End of stack trace from previous location ---\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.gAwaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()\r\n--- End of stack trace from previous location ---\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.gLogged|17_1(ResourceInvoker invoker)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker)\r\n at Umbraco.Cms.Web.Common.Middleware.BasicAuthenticationMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)\r\n at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.InterfaceMiddlewareBinder.<>cDisplayClass2_0.<b0>d.MoveNext()\r\n--- End of stack trace from previous location ---\r\n at Umbraco.Cms.Web.BackOffice.Middleware.BackOfficeExternalLoginProviderErrorMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)\r\n at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.InterfaceMiddlewareBinder.<>cDisplayClass2_0.<b0>d.MoveNext()\r\n--- End of stack trace from previous location ---\r\n at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)\r\n at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)\r\n at Umbraco.Cloud.Cms.PublicAccess.Middleware.CloudPublicAccessMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)\r\n at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.InterfaceMiddlewareBinder.<>cDisplayClass2_0.<b0>d.MoveNext()\r\n--- End of stack trace from previous location ---\r\n at Umbraco.Deploy.Infrastructure.Middleware.BearerTokenAuthenticationMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)\r\n at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.InterfaceMiddlewareBinder.<>cDisplayClass2_0.<b0>d.MoveNext()\r\n--- End of stack trace from previous location ---\r\n at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)\r\n at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)\r\n at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)\r\n at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)\r\n at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)\r\n at StackExchange.Profiling.MiniProfilerMiddleware.Invoke(HttpContext context) in C:\projects\dotnet\src\MiniProfiler.AspNetCore\MiniProfilerMiddleware.cs:line 112\r\n at Umbraco.Cms.Web.Common.Middleware.UmbracoRequestMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)\r\n at Umbraco.Cms.Web.Common.Middleware.UmbracoRequestMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)\r\n at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.InterfaceMiddlewareBinder.<>cDisplayClass2_0.<b0>d.MoveNext()\r\n--- End of stack trace from previous location ---\r\n at Umbraco.Cms.Web.Common.Middleware.PreviewAuthenticationMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)\r\n at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.InterfaceMiddlewareBinder.<>cDisplayClass2_0.<b0>d.MoveNext()\r\n--- End of stack trace from previous location ---\r\n at Umbraco.Cms.Web.Common.Middleware.UmbracoRequestLoggingMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)\r\n at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.InterfaceMiddlewareBinder.<>cDisplayClass2_0.<b0>d.MoveNext()\r\n--- End of stack trace from previous location ---\r\n at SixLabors.ImageSharp.Web.Middleware.ImageSharpMiddleware.Invoke(HttpContext httpContext, Boolean retry)\r\n at Umbraco.Forms.Web.HttpModules.ProtectFormUploadRequestsMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)\r\n at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.InterfaceMiddlewareBinder.<>cDisplayClass2_0.<b__0>d.MoveNext()\r\n--- End of stack trace from previous location ---\r\n at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)", "headers": { "Accept": [ "application/json" ], "Host": [ "localhost:44369" ], "User-Agent": [ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0" ], "Accept-Encoding": [ "gzip, deflate, br, zstd" ], "Accept-Language": [ "en-US,en;q=0.9,da;q=0.8" ], "Cookie": [ "UMB_UPDCHK=1" ], "Referer": [ "https://localhost:44369/umbraco/swagger/index.html?urls.primaryName=Umbraco%20Delivery%20API" ], "sec-ch-ua-platform": [ "\"Windows\"" ], "api-key": [ "set-in-umbraco-cloud-environment-secrets" ], "sec-ch-ua": [ "\"Microsoft Edge\";v=\"129\", \"Not=A?Brand\";v=\"8\", \"Chromium\";v=\"129\"" ], "preview": [ "true" ], "sec-ch-ua-mobile": [ "?0" ], "sec-fetch-site": [ "same-origin" ], "sec-fetch-mode": [ "cors" ], "sec-fetch-dest": [ "empty" ], "priority": [ "u=1, i" ] }, "path": "/umbraco/delivery/api/v2/content", "endpoint": "Umbraco.Cms.Api.Delivery.Controllers.Content.QueryContentApiController.QueryV20 (Umbraco.Cms.Api.Delivery)", "routeValues": { "action": "QueryV20", "controller": "QueryContentApi", "version": "2" } } }

Screenshots of where the issue is happening in code image

The previewdefault is not set to true even through we have sent the corrent query params (api-key and preview) image

This could be caused by the following code not detecting the preview mode correctly and are currently only looking at the cookies and not query params for preview state. However not completely sure😊 image

Steps to reproduce

Steps to Reproduce:

Attempt a GET request to the endpoint /umbraco/delivery/api/v2/content with the fetch=ancestors parameter. Ensure the request targets an unpublished content ID and includes valid authentication headers (API key, preview mode). Observe the 500 Internal Server Error returned by the server.

curl -X 'GET' \ 'https://localhost:44369/umbraco/delivery/api/v2/content?fetch=ancestors%3A6fa39e0b-c9f0-4238-a1f3-dc7eaf846d63&skip=0&take=10&fields=properties%5B%24all%5D' \ -H 'accept: application/json' \ -H 'Api-Key: set-in-umbraco-cloud-environment-secrets' \ -H 'Preview: true'

Expected result / actual result

Actual Behavior: The API returns a 500 Internal Server Error due to a failure in retrieving the content cache, particularly for the unpublished content.

Expected Behavior: The API should successfully return the available ancestor data when preview mode is set to true and has correct preview api key

github-actions[bot] commented 1 month ago

Hi there @acbach!

Firstly, a big thank you for raising this issue. Every piece of feedback we receive helps us to make Umbraco better.

We really appreciate your patience while we wait for our team to have a look at this but we wanted to let you know that we see this and share with you the plan for what comes next.

We wish we could work with everyone directly and assess your issue immediately but we're in the fortunate position of having lots of contributions to work with and only a few humans who are able to do it. We are making progress though and in the meantime, we will keep you in the loop and let you know when we have any questions.

Thanks, from your friendly Umbraco GitHub bot :robot: :slightly_smiling_face:

acbach commented 1 month ago

Workaround middleware for now until I get wiser

public class PreviewMiddleware
{
    private readonly RequestDelegate _next;

    public PreviewMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context, IUmbracoContextFactory umbracoContext,
        IRequestPreviewService requestPreviewService, IApiAccessService apiAccessService)
    {
        if (requestPreviewService.IsPreview() && apiAccessService.HasPreviewAccess())
        {
            var umbraco = umbracoContext.EnsureUmbracoContext();
            umbraco.UmbracoContext.ForcedPreview(true);
        }

        // Call the next delegate/middleware in the pipeline.
        await _next(context);
    }
}
melslenstra-dc commented 1 month ago

We also suffer from this problem, can confirm the workaround works. Adding this because I had to look it up, you can add the middleware in your Composer like so:

            builder.Services.Configure<UmbracoPipelineOptions>(opt =>
                opt.AddFilter(new UmbracoPipelineFilter("PreviewMiddleware", postRouting: app => app.UseMiddleware<PreviewMiddleware>())));

(to be placed inside your Composer