Repository: strapi/strapi
File: packages/core/admin/server/src/controllers/authenticated-user.ts (TypeScript)

Code before:
          try {
            errorText = await response.text();
            errorData = JSON.parse(errorText);
          } catch {
            errorData = { error: errorText || 'Failed to parse error response' };
          }

          strapi.log.error('AI token request failed:', {
            status: response.status,
            statusText: response.statusText,
            error: errorData,
            errorText,
            projectId,
            aiServerUrl,
          });

          // Return more specific error messages
          if (response.status === 401) {
            // This shouldn't happen with the public endpoint
            const message =
              process.env.NODE_ENV === 'development'
                ? `AI server request failed. Response: ${errorText || 'No response body'}.`
                : 'AI server request failed.';
            return ctx.unauthorized(message);
          }
          if (response.status === 403) {
            return ctx.forbidden('License validation failed. Check your EE license.');
          }
          if (response.status === 404) {
            return ctx.notFound(`AI service endpoint not found at ${aiServerUrl}`);
          }

          // Include more detail in error for debugging
          return ctx.badRequest(
            `Failed to obtain AI token. Server responded with: ${response.status} ${errorText || response.statusText}`
          );
        }

        const data = (await response.json()) as {
          jwt: string;
          expiresAt?: string;
          projectId?: string;
        };

        if (!data.jwt) {
          throw new Error('Invalid response: missing JWT token');
        }

        strapi.log.info('AI token generated successfully', {
          userId: user.id,
          projectId: data.projectId,

Review comment (suggestion):
On all of these (not just these two) we need to review if this is information the server should be telling the user, or if it should be put in the server log and sent to the user as a more generic error.

@maccomaccomaccomacco might have some insight here

Code after:
          }

          strapi.log.error('AI token request failed:', {
            status: response.status,
            statusText: response.statusText,
            error: errorData,
            errorText,
            projectId,
            aiServerUrl,
          });

          // Return more specific error messages
          if (response.status === 401) {
            // This shouldn't happen with the public endpoint
            const message =
              process.env.NODE_ENV === 'development'
                ? `AI server request failed. Response: ${errorText || 'No response body'}.`
                : 'AI server request failed.';
            return ctx.unauthorized(message);
          }
          if (response.status === 403) {
            return ctx.forbidden('License validation failed. Check your EE license.');
          }
          if (response.status === 404) {
            return ctx.notFound(`AI service endpoint not found at ${aiServerUrl}`);
          }

          // Include more detail in error for debugging
          return ctx.badRequest(
            `Failed to obtain AI token. Server responded with: ${response.status} ${errorText || response.statusText}`
          );
        }

        const data = (await response.json()) as {
          jwt: string;
          expiresAt?: string;
        };

        if (!data.jwt) {
          throw new Error('Invalid response: missing JWT token');
        }

        strapi.log.info('AI token generated successfully', {
          userId: user.id,
          expiresAt: data.expiresAt,
        });

        // Return the AI JWT with metadata
        // Note: Token expires in 1 hour, client should handle refresh
        ctx.body = {
          data: {