一键导入
http-status-aware-error-handling
Graceful HTTP error handling in Web controllers with status-code-specific user messaging
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Graceful HTTP error handling in Web controllers with status-code-specific user messaging
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | http-status-aware-error-handling |
| description | Graceful HTTP error handling in Web controllers with status-code-specific user messaging |
| domain | error-handling, web-ux |
| confidence | high |
| source | earned (Issue #708 fix) |
ASP.NET Core Web controllers calling downstream APIs via HttpClient should differentiate HTTP error responses based on status code, providing appropriate user feedback:
This applies when:
try
{
var result = await _service.AddResourceAsync(id, data);
if (result is null)
{
TempData["ErrorMessage"] = "Failed to add resource.";
}
else
{
TempData["SuccessMessage"] = "Resource added successfully.";
}
}
catch (HttpRequestException ex)
{
// Differentiate based on status code
if (ex.StatusCode == System.Net.HttpStatusCode.Conflict)
{
TempData["WarningMessage"] = "This resource is already associated.";
}
else if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
{
TempData["ErrorMessage"] = "The target resource was not found.";
}
else
{
TempData["ErrorMessage"] = $"Failed to add resource: {ex.Message}";
}
}
Ensure _Layout.cshtml (or equivalent) displays warning-level messages:
@if (TempData["SuccessMessage"] != null)
{
<div class="alert alert-success alert-dismissible fade show" role="alert">
@TempData["SuccessMessage"]
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
}
@if (TempData["WarningMessage"] != null)
{
<div class="alert alert-warning alert-dismissible fade show" role="alert">
@TempData["WarningMessage"]
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
}
@if (TempData["ErrorMessage"] != null)
{
<div class="alert alert-danger alert-dismissible fade show" role="alert">
@TempData["ErrorMessage"]
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
}
[Fact]
public async Task Action_When409Conflict_RedirectsWithWarningMessage()
{
// Arrange
var conflictException = new HttpRequestException(
"Conflict",
null,
System.Net.HttpStatusCode.Conflict);
_service.Setup(s => s.AddAsync(1, 2))
.ThrowsAsync(conflictException);
// Act
var result = await _controller.Add(viewModel);
// Assert
var redirectResult = Assert.IsType<RedirectToActionResult>(result);
Assert.Equal("This resource is already associated.", _controller.TempData["WarningMessage"]);
}
Problem: Adding a social media platform to an engagement returns 409 if already associated. Original code showed generic error.
Solution:
TempData["WarningMessage"] = "This platform is already associated with this engagement."TempData["ErrorMessage"] with technical detailsFiles:
src/JosephGuadagno.Broadcasting.Web/Controllers/EngagementsController.cs (lines 250-280)src/JosephGuadagno.Broadcasting.Web/Views/Shared/_Layout.cshtml (lines 148-161)src/JosephGuadagno.Broadcasting.Web.Tests/Controllers/EngagementsControllerTests.cs (AddPlatform tests)catch (HttpRequestException ex)
{
TempData["ErrorMessage"] = $"Failed: {ex.Message}"; // ← No differentiation
}
Problem: User can't tell if retry is safe or if something is broken.
if (ex.StatusCode == HttpStatusCode.Conflict)
{
// Silent ignore ← User has no feedback
}
Problem: User doesn't know the operation was a no-op.
if (ex.StatusCode == HttpStatusCode.Conflict)
{
TempData["SuccessMessage"] = "Resource added successfully."; // ← Misleading
}
Problem: User thinks resource was just added when it was already there.
| Status Code | Level | User Message Example | Retry Safe? |
|---|---|---|---|
| 200/201/204 | Success | "Resource added successfully." | N/A |
| 400 | Error | "Invalid request: {details}" | No (fix data) |
| 404 | Error | "Resource not found." | No |
| 409 | Warning | "This resource is already associated." | Yes (no-op) |
| 422 | Error | "Validation failed: {details}" | No (fix data) |
| 500 | Error | "An error occurred. Please try again later." | Maybe |
Authorization Code Flow for web applications using MSAL.NET confidential client to sign in users and access APIs on their behalf
Handle MSAL distributed token cache collisions and stale entries in ASP.NET Core applications
On-Behalf-Of (OBO) Flow for web APIs to call downstream APIs while preserving user identity in MSAL.NET
{what this skill teaches agents}
Review API DTO implementations for contract/domain separation, mapping patterns, and REST compliance
This skill should be used when the user asks to "build a feature", "fix a bug", "implement something", "start a dev cycle", types "/dev", or describes a software task that requires design, implementation, testing, and shipping. Orchestrates the full software development lifecycle from interrogation through shipping, with self-learning that improves over time.