Skip to content

Conversation

@Omesh2004
Copy link
Contributor

@Omesh2004 Omesh2004 commented Oct 2, 2025

Summary

Describe the change and its motivation.
i have added the recover excercise without merge conflicts

Checklist

Screenshots / Output (if CLI UX)

Paste before/after where helpful.

Related issues

Fixes #

Summary by CodeRabbit

  • New Features
    • Added a new exercise "Safe Panic Recovery" with three guided hints.
    • Improved discovery so missing embedded exercises are synthesized at runtime.
  • Tests
    • Added tests validating normal execution and proper panic recovery, asserting recovered values.
  • Chores
    • Updated metadata for the existing "Epoch Conversion" exercise.

@coderabbitai
Copy link

coderabbitai bot commented Oct 2, 2025

Walkthrough

Adds a new exercise "Safe Panic Recovery" (slug 110_recover) with template, tests, and reference solution demonstrating defer+recover; updates 109_epoch catalog entry to use test_regex: ".*"; and adds a fallback in Get(slug) to synthesize an Exercise when a template/solution exists but catalog entry is missing.

Changes

Cohort / File(s) Summary
Catalog updates
internal/exercises/catalog.yaml
Adds exercise entry for 110_recover ("Safe Panic Recovery") with test_regex: ".*" and three hints (concept, structure, solution). Replaces 109_epoch difficulty/topics with test_regex: ".*".
Templates & tests
internal/exercises/templates/110_recover/recover.go, internal/exercises/templates/110_recover/recover_test.go
Adds template package recover_exercise with DoWork(n int) (panics if n < 0) and skeleton Run(n int) (recoveredValue interface{}) intended to use defer+recover. Adds tests asserting Run(10)nil and Run(-5) recovers "input cannot be negative".
Reference solution
internal/exercises/solutions/110_recover/recover.go
Adds solution implementing DoWork and Run, where Run uses a deferred function calling recover() to capture and return any panic value.
Exercise discovery
internal/exercises/exercises.go
Adds fallback in Get(slug) to synthesize and return an Exercise (slug/title derived from slug, test_regex: ".*", hint indicating missing catalog metadata) when templates/solutions exist but catalog/local entry is absent.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Caller
  participant Run as Run(n)
  participant DoWork as DoWork(n)

  Caller->>Run: call with n
  activate Run
  note right of Run #E6F7FF: deferred function calls recover()
  Run->>DoWork: invoke DoWork(n)
  alt n < 0
    DoWork--x Run: panic("input cannot be negative")
    note right of Run #FFF4E6: deferred recover captures panic value
  else n >= 0
    DoWork-->>Run: return normally
  end
  Run-->>Caller: return recoveredValue (nil or panic value)
  deactivate Run
Loading
sequenceDiagram
  autonumber
  actor Client
  participant Get as Get(slug)
  participant Catalog as catalog.yaml
  participant Local as local list
  participant Templates as embedded templates/solutions

  Client->>Get: request slug
  Get->>Catalog: lookup slug
  alt found in catalog
    Catalog-->>Get: Exercise entry
  else not in catalog
    Get->>Local: lookup slug
    alt found locally
      Local-->>Get: Exercise entry
    else not found locally
      Get->>Templates: check embedded template/solution
      alt template/solution exists
        Templates-->>Get: synthesize Exercise (slug/title, test_regex ".*")
      else none
        Get-->>Client: not-found error
      end
    end
  end
  Get-->>Client: Exercise (entry or synthesized) / error
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

patch

Poem

I hop through code with nimble paws,
I catch wild panics without a pause.
Defer my whiskers, recover the scare,
Return the surprise with gentle care.
Tests pass green — I nibble a carrot there 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title references the new recover exercise but is misspelled, includes filler text, and does not clearly summarize the main changes. It partially relates to the addition of the exercise but fails to convey the specific intent or content of the PR. A concise, descriptive title would improve clarity and context for reviewers.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e01e794 and 3338c71.

📒 Files selected for processing (1)
  • internal/exercises/exercises.go (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/exercises/exercises.go

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Omesh2004
Copy link
Contributor Author

pls review now

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 787d661 and 89ad530.

📒 Files selected for processing (1)
  • internal/exercises/catalog.yaml (1 hunks)

Comment on lines +206 to +211
- hint_type: concept
text: The `recover()` function is ONLY useful inside a function that is executed by a `defer` statement. Outside of a deferred function, `recover` will return `nil` and have no effect.
- hint_type: structure
text: You must place a `defer` statement at the beginning of the `Run` function. The function passed to `defer` is where you should call `recover()`.
- hint_type: solution
text: "The core logic of the deferred function should look like this: `if r := recover(); r != nil { recoveredValue = r }`"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Restore string hints to keep catalog schema valid

Every existing catalog entry provides hints as plain strings. The loader expects a []string; supplying maps (hint_type, text) will fail to unmarshal and break catalog loading. Please keep hints as simple strings (or extend the loader before changing the schema).

-    - hint_type: concept
-      text: The `recover()` function is ONLY useful inside a function that is executed by a `defer` statement. Outside of a deferred function, `recover` will return `nil` and have no effect.
-    - hint_type: structure
-      text: You must place a `defer` statement at the beginning of the `Run` function. The function passed to `defer` is where you should call `recover()`.
-    - hint_type: solution
-      text: "The core logic of the deferred function should look like this: `if r := recover(); r != nil { recoveredValue = r }`"
+    - The `recover()` function is ONLY useful inside a function that is executed by a `defer` statement. Outside of a deferred function, `recover` will return `nil` and have no effect.
+    - Place a `defer` statement at the beginning of `Run`; call `recover()` inside the deferred function.
+    - The deferred function can follow: `if r := recover(); r != nil { recoveredValue = r }`
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- hint_type: concept
text: The `recover()` function is ONLY useful inside a function that is executed by a `defer` statement. Outside of a deferred function, `recover` will return `nil` and have no effect.
- hint_type: structure
text: You must place a `defer` statement at the beginning of the `Run` function. The function passed to `defer` is where you should call `recover()`.
- hint_type: solution
text: "The core logic of the deferred function should look like this: `if r := recover(); r != nil { recoveredValue = r }`"
- The `recover()` function is ONLY useful inside a function that is executed by a `defer` statement. Outside of a deferred function, `recover` will return `nil` and have no effect.
- Place a `defer` statement at the beginning of `Run`; call `recover()` inside the deferred function.
- The deferred function can follow: `if r := recover(); r != nil { recoveredValue = r }`
🤖 Prompt for AI Agents
In internal/exercises/catalog.yaml around lines 206 to 211, the hints are
defined as maps with keys like hint_type/text which breaks the loader expecting
a []string; replace those three map entries with simple string items containing
the hint text (i.e., convert each map to a plain string entry using the existing
text values) so the hints remain a list of strings and the catalog loader can
unmarshal them correctly.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 89ad530 and e01e794.

📒 Files selected for processing (1)
  • internal/exercises/exercises.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
internal/exercises/exercises.go (1)
internal/exercises/solutions.go (1)
  • SolutionExists (18-22)

Comment on lines +128 to +139
// Fallback: if an embedded template or solution exists, synthesize an Exercise entry
if templateExists(slug) || SolutionExists(slug) {
fmt.Fprintf(os.Stderr,
"Warning: exercise '%s' found in templates/solutions but missing from catalog.yaml\n",
slug,
)
return Exercise{
Slug: slug,
Title: formatSlugAsTitle(slug), // e.g., "110 Recover"
TestRegex: ".*",
Hints: []string{"This exercise is missing proper catalog metadata. Check documentation."},
}, nil
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Omesh2004 : Why do we need support this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatSlugAsTitle helper is only used in the fallback path when the slug isn’t in catalog.yaml (and not in local ./exercises) but exists in embedded templates/solutions..

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice edition, could you add this as separate PR?

Comment on lines +204 to +235
func formatSlugAsTitle(slug string) string {
s := strings.TrimSpace(slug)
if s == "" {
return "Exercise"
}
parts := strings.Split(s, "_")
for i, p := range parts {
if p == "" {
continue
}
// Keep purely numeric segments as-is (e.g., "110")
isDigits := true
for _, r := range p {
if r < '0' || r > '9' {
isDigits = false
break
}
}
if isDigits {
parts[i] = p
continue
}
upper := strings.ToUpper(p)
switch upper {
case "JSON", "XML", "HTTP", "CLI", "KV", "ID", "URL", "IO":
parts[i] = upper
default:
parts[i] = strings.ToUpper(p[:1]) + strings.ToLower(p[1:])
}
}
return strings.Join(parts, " ")
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate what are we trying to achieve here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically this helper function is alloting titles based on the slugs in case the same arent present in catalog.yaml file

110_recover -> 110 Recover
36_json -> 36 JSON
104_http_server -> 104 HTTP Server
105_cli_todo_list -> 105 CLI Todo List are some of the examples for human readable titles

@kaushalyap
Copy link
Collaborator

@Omesh2004 Please do not do multiple things in single PR, keep this PR focused on recover exercise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants