feat: add informative REST results for TAKE failures (SOFIE-295) #1621
feat: add informative REST results for TAKE failures (SOFIE-295) #1621anteeek merged 2 commits intoSofie-Automation:mainfrom
Conversation
WalkthroughAdds a Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant REST as REST API (Playlists)
participant Server as PlaylistsServerAPI
participant Jobs as StudioJobs
participant Worker as JobWorker (handleTakeNextPart)
Client->>REST: POST /playlists/:id/take
REST->>Server: invoke take(...)
Server->>Jobs: StudioJobs.TakeNextPart(...)
Jobs->>Worker: schedule/execute take handling
Worker-->>Jobs: TakeNextPartResult { nextTakeTime }
Jobs-->>Server: return TakeNextPartResult
Server-->>REST: respond 200 with TakeNextPartResult
REST-->>Client: 200 { nextTakeTime } / or error with additionalInfo
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
f50e077 to
0b0ef4c
Compare
|
Note: this PR was first created with wrong target in bbc#64 |
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
Julusian
left a comment
There was a problem hiding this comment.
I feel like the openapi definitions should be updated to reflect this addition?
I don't see how the time is being reported for non-200 status codes. Unless the nextAllowedTakeTime inside the UserError is being reported somehow?
0b0ef4c to
4a86d64
Compare
|
@Julusian addressed the comment, thanks! Please take a look when you can |
| */ | ||
| export function responseError(userError: UserError): ClientResponseError { | ||
| return { error: UserError.serialize(userError), errorCode: userError.errorCode } | ||
| const nextAllowedTakeTime = userError.userMessage.args?.nextAllowedTakeTime as number | undefined |
There was a problem hiding this comment.
I'm not keen on this.
If its going to be a concept that we have defined in the client api, then it should be a proper concept in the UserError too.
But I'm not sure if it should be a dedicated value in ClientResponseError either.
I don't have any suggestions on how to do this better though, it just feels smelly to have this value specific to one UserAction be present in what is otherwise very general code
There was a problem hiding this comment.
I suppose I wouldnt mind if on UserError has an explicit property for nextAllowedOperationTime, and that was on ClientResponseError too. (or something named more around being a rate limit)
Then at least it is more generic and could be used by other things too (even though it wont for now, but maybe there should be more cases where we 'rate limit')
In the api it can continue to call it nextAllowedTakeTime, I am just worried about the specific naming of something for a single UserAction on internal types (if every action did that, these types would become a confusing mess
There was a problem hiding this comment.
@Julusian made it more generic, take a look please
4a86d64 to
fc72f02
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@packages/openapi/api/definitions/playlists.yaml`:
- Around line 623-656: Replace the non-standard 425 response object with a 409
response: rename the top-level key 425 to 409, change its description to
indicate a Conflict (take blocked due to transition/adlib), and update the
example status value to 409; for both the 409 (formerly 425) and 429 response
schemas add a required: [status, message, nextAllowedTakeTime] array and ensure
the properties status, message and nextAllowedTakeTime remain defined (and keep
their examples/descriptions).
🧹 Nitpick comments (2)
packages/meteor-lib/src/api/client.ts (1)
53-54: Extracting a domain-specific field from untyped error args is pragmatic but fragile.The
nextAllowedTakeTimeis extracted fromuserError.userMessage.argsvia a cast tonumber | undefined. Sinceargsis essentiallyRecord<string, any>, there's no compile-time guarantee that the value is actually a number. This works because the callers intake.tsalways pass a number, but it could silently propagate incorrect types if a caller passes something else.Consider adding a runtime guard (e.g.,
typeof ... === 'number') to be safe:Suggested defensive check
- const nextAllowedTakeTime = userError.userMessage.args?.nextAllowedTakeTime as number | undefined + const rawNextAllowedTakeTime = userError.userMessage.args?.nextAllowedTakeTime + const nextAllowedTakeTime = typeof rawNextAllowedTakeTime === 'number' ? rawNextAllowedTakeTime : undefinedAlso applies to: 64-69
packages/job-worker/src/playout/take.ts (1)
29-29: Duplicate import from the same module.
TakeNextPartResult(line 43) is imported separately fromTakeNextPartProps(line 29), both from@sofie-automation/corelib/dist/worker/studio. Consolidate into a single import statement.Suggested fix
At line 29, merge the imports:
-import { TakeNextPartProps } from '@sofie-automation/corelib/dist/worker/studio' +import { TakeNextPartProps, TakeNextPartResult } from '@sofie-automation/corelib/dist/worker/studio'Then remove lines 43-44.
Also applies to: 43-44
About the Contributor
This PR is made on behalf of the BBC
Type of Contribution
This is a:
Feature
Current Behavior
The TAKE endpoint returns either 200 or 500, without any additional information
New Behavior
HTTP Rest codes 400, 425 and 429 are added as possible failure reasons, and a UTC timestamp for next possible take is added
Testing
Affected areas
the TAKE endpoint
Time Frame
Not urgent, but we would like to get this merged into the in-development release.
Other Information
Status
This PR adds informative REST results for TAKE failures by extending the TAKE endpoint to return additional HTTP status codes, include timing information in responses, and propagate additional error details to clients.
Changes
New Data Types
TakeNextPartResultinpackages/corelib/src/worker/studio.tswithnextTakeTime: numberto communicate the next available TAKE time.Enhanced Error Handling & Return Values
handleTakeNextPartinpackages/job-worker/src/playout/take.tsnow returnsPromise<TakeNextPartResult>and suppliesnextTakeTimeon success.nextAllowedTakeTime.nextAllowedTakeTime.TakeBlockedDuration,TakeDuringTransition,TakeCloseToAutonext, rate-limit) now attachnextAllowedTakeTimein their error objects.API Surface Updates
TakeNextPartResult:PlaylistsServerAPI.takeinmeteor/server/api/rest/v1/playlists.ts:ClientAPI.ClientResponse<TakeNextPartResult>.PlaylistsRestAPI.takeinmeteor/server/lib/rest/v1/playlists.ts:Promise<ClientAPI.ClientResponse<TakeNextPartResult>>.NewUserActionAPI.takeinpackages/meteor-lib/src/api/userActions.ts:Promise<ClientAPI.ClientResponse<TakeNextPartResult>>.ClientAPI.ClientResponseErrorinpackages/meteor-lib/src/api/client.tsnow includes an optionaladditionalInfo?: Record<string, unknown>, andresponseErrorpopulates this from user error args when present.meteor/server/api/rest/v1/index.ts) now accepts and forwardsadditionalInfofrom client errors into HTTP responses.OpenAPI Documentation
packages/openapi/api/definitions/playlists.yamlupdated:nextTakeTime.additionalInfo(e.g.nextAllowedTakeTime).Tests
packages/meteor-lib/src/api/__tests__/client.test.tsto verify extraction and propagation ofadditionalInfo(includingnextAllowedTakeTime) when present, and absence when not provided.Impact
Clients calling the TAKE endpoint now receive:
nextTakeTime/nextAllowedTakeTimeinadditionalInfo) indicating when the next TAKE is allowed, enabling smarter client retry behavior.