Skip to content

Exception Handling

Art Admin uses typed exceptions with global middleware that automatically converts them to standard JSON responses.

Exception Types

ExceptionHTTP StatusUsage
BadRequestException400Parameter error, business rule violation
UnauthorizedException401Not logged in, Token expired
ForbiddenException403No permission
NotFoundException404Resource not found
InternalServerException500Internal error

Usage

csharp
public async Task<UserDto> GetUserAsync(long id)
{
    var user = await _db.SysUsers.FindAsync(id);
    if (user == null)
        throw new NotFoundException("User not found");

    if (user.Status != 1)
        throw new BadRequestException("User is disabled");

    return new UserDto { ... };
}

Coding Style

Single-line if without braces:

csharp
// ✅ Correct
if (user == null)
    throw new NotFoundException("User not found");

// ❌ Wrong
if (user == null) {
    throw new NotFoundException("User not found");
}

Response Format

The global exception middleware catches all typed exceptions and returns:

json
{
  "code": 404,
  "msg": "User not found"
}

Successful Response

Normal returns are automatically wrapped:

json
{
  "code": 200,
  "data": { ... }
}

Frontend Integration

The frontend HTTP client (src/utils/http) has global ElMessage.error interception. Do not add duplicate error messages in catch blocks — just restore state:

typescript
try {
  await fetchSaveUser(formData);
} catch (e) {
  // ❌ Don't do this — duplicate toast
  // ElMessage.error('Save failed');

  // ✅ Just restore state
  loading.value = false;
}