Add job error persistence and friendly error UI
Backend: - Migration 0003_job_error: adds error_message and error_code columns to jobs table - models/job.go: add ErrorMessage, ErrorCode fields to Job struct; SetError method; update all SELECT queries - models/job_log.go: GetAllFiltered also reads error_message and error_code (via Job embed) - syncengine/engine.go: setJobError() helper; capture errors at Wol timeout (wol_timeout), rsync error (rsync_error), and exit_code failure points - api/dto.go: add ErrorMessage and ErrorCode to JobResponse - api/handlers_jobs.go: jobToResp propagates error fields Frontend: - api/client.ts: add error_message? and error_code? to Job interface - lib/status.ts: add ERROR_CODES map with friendly titles/hints; getErrorCodeInfo() - components/ErrorDetailsModal.tsx: new modal showing error title, hint, full message, job metadata, and stderr log; copy-all and download-log buttons - pages/JobDetail.tsx: error banner for failed jobs with title/hint; View error button opens ErrorDetailsModal; SSE updates error_message in real-time
This commit is contained in:
+46
-17
@@ -6,14 +6,16 @@ import (
|
||||
)
|
||||
|
||||
type Job struct {
|
||||
ID int64 `db:"id" json:"id"`
|
||||
SyncPairID int64 `db:"sync_pair_id" json:"sync_pair_id"`
|
||||
TriggerType string `db:"trigger_type" json:"trigger_type"`
|
||||
Status string `db:"status" json:"status"`
|
||||
StartedAt *time.Time `db:"started_at" json:"started_at"`
|
||||
FinishedAt *time.Time `db:"finished_at" json:"finished_at"`
|
||||
LogFile *string `db:"log_file" json:"log_file"`
|
||||
CreatedAt time.Time `db:"created_at" json:"created_at"`
|
||||
ID int64 `db:"id" json:"id"`
|
||||
SyncPairID int64 `db:"sync_pair_id" json:"sync_pair_id"`
|
||||
TriggerType string `db:"trigger_type" json:"trigger_type"`
|
||||
Status string `db:"status" json:"status"`
|
||||
StartedAt *time.Time `db:"started_at" json:"started_at"`
|
||||
FinishedAt *time.Time `db:"finished_at" json:"finished_at"`
|
||||
LogFile *string `db:"log_file" json:"log_file"`
|
||||
ErrorMessage *string `db:"error_message" json:"error_message,omitempty"`
|
||||
ErrorCode *string `db:"error_code" json:"error_code,omitempty"`
|
||||
CreatedAt time.Time `db:"created_at" json:"created_at"`
|
||||
}
|
||||
|
||||
type JobRepository struct {
|
||||
@@ -38,12 +40,12 @@ func (r *JobRepository) Create(syncPairID int64, triggerType, status string) (in
|
||||
func (r *JobRepository) GetByID(id int64) (*Job, error) {
|
||||
var j Job
|
||||
var started, finished sql.NullTime
|
||||
var logFile sql.NullString
|
||||
var logFile, errMsg, errCode sql.NullString
|
||||
err := r.db.QueryRow(`
|
||||
SELECT id, sync_pair_id, trigger_type, status, started_at, finished_at,
|
||||
log_file, created_at FROM jobs WHERE id = ?`, id).Scan(
|
||||
log_file, error_message, error_code, created_at FROM jobs WHERE id = ?`, id).Scan(
|
||||
&j.ID, &j.SyncPairID, &j.TriggerType, &j.Status, &started, &finished,
|
||||
&logFile, &j.CreatedAt)
|
||||
&logFile, &errMsg, &errCode, &j.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -56,13 +58,20 @@ func (r *JobRepository) GetByID(id int64) (*Job, error) {
|
||||
if logFile.Valid {
|
||||
j.LogFile = &logFile.String
|
||||
}
|
||||
if errMsg.Valid {
|
||||
j.ErrorMessage = &errMsg.String
|
||||
}
|
||||
if errCode.Valid {
|
||||
j.ErrorCode = &errCode.String
|
||||
}
|
||||
return &j, nil
|
||||
}
|
||||
|
||||
func (r *JobRepository) GetAll(limit, offset int) ([]Job, error) {
|
||||
rows, err := r.db.Query(`
|
||||
SELECT id, sync_pair_id, trigger_type, status, started_at, finished_at,
|
||||
log_file, created_at FROM jobs ORDER BY created_at DESC LIMIT ? OFFSET ?`,
|
||||
log_file, error_message, error_code, created_at
|
||||
FROM jobs ORDER BY created_at DESC LIMIT ? OFFSET ?`,
|
||||
limit, offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -73,9 +82,9 @@ func (r *JobRepository) GetAll(limit, offset int) ([]Job, error) {
|
||||
for rows.Next() {
|
||||
var j Job
|
||||
var started, finished sql.NullTime
|
||||
var logFile sql.NullString
|
||||
var logFile, errMsg, errCode sql.NullString
|
||||
if err := rows.Scan(&j.ID, &j.SyncPairID, &j.TriggerType, &j.Status,
|
||||
&started, &finished, &logFile, &j.CreatedAt); err != nil {
|
||||
&started, &finished, &logFile, &errMsg, &errCode, &j.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if started.Valid {
|
||||
@@ -87,6 +96,12 @@ func (r *JobRepository) GetAll(limit, offset int) ([]Job, error) {
|
||||
if logFile.Valid {
|
||||
j.LogFile = &logFile.String
|
||||
}
|
||||
if errMsg.Valid {
|
||||
j.ErrorMessage = &errMsg.String
|
||||
}
|
||||
if errCode.Valid {
|
||||
j.ErrorCode = &errCode.String
|
||||
}
|
||||
jobs = append(jobs, j)
|
||||
}
|
||||
return jobs, rows.Err()
|
||||
@@ -115,17 +130,25 @@ func (r *JobRepository) SetLogFile(id int64, path string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *JobRepository) SetError(id int64, code, message string) error {
|
||||
_, err := r.db.Exec(
|
||||
"UPDATE jobs SET error_code = ?, error_message = ? WHERE id = ?",
|
||||
code, message, id,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *JobRepository) GetRunningBySyncPair(syncPairID int64) (*Job, error) {
|
||||
var j Job
|
||||
var started sql.NullTime
|
||||
var logFile sql.NullString
|
||||
var logFile, errMsg, errCode sql.NullString
|
||||
err := r.db.QueryRow(`
|
||||
SELECT id, sync_pair_id, trigger_type, status, started_at, finished_at,
|
||||
log_file, created_at FROM jobs
|
||||
log_file, error_message, error_code, created_at FROM jobs
|
||||
WHERE sync_pair_id = ? AND status IN ('queued','waking_up','running')
|
||||
ORDER BY created_at DESC LIMIT 1`, syncPairID).Scan(
|
||||
&j.ID, &j.SyncPairID, &j.TriggerType, &j.Status, &started,
|
||||
&j.FinishedAt, &logFile, &j.CreatedAt)
|
||||
&j.FinishedAt, &logFile, &errMsg, &errCode, &j.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -135,6 +158,12 @@ func (r *JobRepository) GetRunningBySyncPair(syncPairID int64) (*Job, error) {
|
||||
if logFile.Valid {
|
||||
j.LogFile = &logFile.String
|
||||
}
|
||||
if errMsg.Valid {
|
||||
j.ErrorMessage = &errMsg.String
|
||||
}
|
||||
if errCode.Valid {
|
||||
j.ErrorCode = &errCode.String
|
||||
}
|
||||
return &j, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -124,7 +124,8 @@ func (r *JobLogRepository) GetAllFiltered(limit, offset int, syncPairID *int64,
|
||||
query := `
|
||||
SELECT
|
||||
j.id, j.sync_pair_id, j.trigger_type, j.status,
|
||||
j.started_at, j.finished_at, j.log_file, j.created_at,
|
||||
j.started_at, j.finished_at, j.log_file,
|
||||
j.error_message, j.error_code, j.created_at,
|
||||
CASE WHEN j.finished_at IS NOT NULL AND j.started_at IS NOT NULL
|
||||
THEN (j.finished_at - j.started_at) ELSE NULL END as duration_seconds,
|
||||
(SELECT COUNT(*) FROM job_logs WHERE job_id = j.id) as log_line_count
|
||||
@@ -143,10 +144,11 @@ func (r *JobLogRepository) GetAllFiltered(limit, offset int, syncPairID *int64,
|
||||
for rows.Next() {
|
||||
var j JobWithStats
|
||||
var started, finished sql.NullTime
|
||||
var logFile sql.NullString
|
||||
var logFile, errMsg, errCode sql.NullString
|
||||
var durationSeconds sql.NullInt64
|
||||
if err := rows.Scan(&j.ID, &j.SyncPairID, &j.TriggerType, &j.Status,
|
||||
&started, &finished, &logFile, &j.CreatedAt,
|
||||
&started, &finished, &logFile,
|
||||
&errMsg, &errCode, &j.CreatedAt,
|
||||
&durationSeconds, &j.LogLineCount); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -159,6 +161,12 @@ func (r *JobLogRepository) GetAllFiltered(limit, offset int, syncPairID *int64,
|
||||
if logFile.Valid {
|
||||
j.LogFile = &logFile.String
|
||||
}
|
||||
if errMsg.Valid {
|
||||
j.ErrorMessage = &errMsg.String
|
||||
}
|
||||
if errCode.Valid {
|
||||
j.ErrorCode = &errCode.String
|
||||
}
|
||||
if durationSeconds.Valid {
|
||||
j.DurationSeconds = &durationSeconds.Int64
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user