Skip to content

Commit d6fb769

Browse files
committed
feat: handle error for downloading and extrating
1 parent 4c850b8 commit d6fb769

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

models/weaveinit/run_l1_node.go

+17-11
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ func NewExistingDataReplaceSelect(state *RunL1NodeState) *ExistingDataReplaceSel
932932
},
933933
},
934934
state: state,
935-
question: fmt.Sprintf("Existing %s detected. Would you like to use the current one or replace it", utils.InitiaDataDirectory),
935+
question: fmt.Sprintf("Existing %s detected. Would you like to use the current one or replace it?", utils.InitiaDataDirectory),
936936
}
937937
}
938938

@@ -998,7 +998,8 @@ func (m *SnapshotEndpointInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
998998
input, cmd, done := m.TextInput.Update(msg)
999999
if done {
10001000
m.state.snapshotEndpoint = input.Text
1001-
// m.state.weave.PreviousResponse += styles.RenderPreviousResponse(styles.DotsSeparator, m.GetQuestion(), []string{"snapshot url"}, input.Text)
1001+
m.state.weave.PushPreviousResponse(styles.RenderPreviousResponse(styles.DotsSeparator, m.GetQuestion(), []string{"snapshot url"}, input.Text))
1002+
10021003
if snapshotDownload, err := NewSnapshotDownloadLoading(m.state); err == nil {
10031004
return snapshotDownload, snapshotDownload.Init()
10041005
} else {
@@ -1065,8 +1066,7 @@ type SnapshotDownloadLoading struct {
10651066
func NewSnapshotDownloadLoading(state *RunL1NodeState) (*SnapshotDownloadLoading, error) {
10661067
userHome, err := os.UserHomeDir()
10671068
if err != nil {
1068-
fmt.Printf("[error] Failed to get user home: %v\n", err)
1069-
return nil, err
1069+
return nil, fmt.Errorf("[error] Failed to get user home: %v", err)
10701070
}
10711071

10721072
return &SnapshotDownloadLoading{
@@ -1087,7 +1087,7 @@ func (m *SnapshotDownloadLoading) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
10871087
if err := m.GetError(); err != nil {
10881088
model := NewSnapshotEndpointInput(m.state)
10891089
model.err = err
1090-
return model, nil
1090+
return model, model.Init()
10911091
}
10921092

10931093
if m.GetCompletion() {
@@ -1104,8 +1104,7 @@ func (m *SnapshotDownloadLoading) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
11041104
}
11051105

11061106
func (m *SnapshotDownloadLoading) View() string {
1107-
view := m.state.weave.Render() + m.Downloader.View()
1108-
return view
1107+
return m.state.weave.Render() + m.Downloader.View()
11091108
}
11101109

11111110
type SnapshotExtractLoading struct {
@@ -1127,6 +1126,15 @@ func (m *SnapshotExtractLoading) Init() tea.Cmd {
11271126
func (m *SnapshotExtractLoading) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
11281127
loader, cmd := m.Loading.Update(msg)
11291128
m.Loading = loader
1129+
switch msg := msg.(type) {
1130+
case utils.ErrorLoading:
1131+
m.state.weave.PopPreviousResponse()
1132+
m.state.weave.PopPreviousResponse()
1133+
model := NewSnapshotEndpointInput(m.state)
1134+
model.err = msg.Err
1135+
return model, cmd
1136+
}
1137+
11301138
if m.Loading.Completing {
11311139
m.state.weave.PushPreviousResponse(styles.RenderPreviousResponse(styles.NoSeparator, fmt.Sprintf("Snapshot extracted to %s successfully.", utils.InitiaDataDirectory), []string{}, ""))
11321140
return m, tea.Quit
@@ -1145,8 +1153,7 @@ func snapshotExtractor() tea.Cmd {
11451153
return func() tea.Msg {
11461154
userHome, err := os.UserHomeDir()
11471155
if err != nil {
1148-
fmt.Printf("[error] Failed to get user home: %v\n", err)
1149-
// TODO: Return error
1156+
return utils.ErrorLoading{Err: fmt.Errorf("[error] Failed to get user home: %v", err)}
11501157
}
11511158

11521159
targetDir := filepath.Join(userHome, utils.InitiaDirectory)
@@ -1156,8 +1163,7 @@ func snapshotExtractor() tea.Cmd {
11561163

11571164
err = cmd.Run()
11581165
if err != nil {
1159-
fmt.Printf("[error] Failed to extract snapshot: %v\n", err)
1160-
// TODO: Return error
1166+
return utils.ErrorLoading{Err: fmt.Errorf("[error] Failed to extract snapshot: %v", err)}
11611167
}
11621168
return utils.EndLoading{}
11631169
}

utils/downloader.go

+38
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package utils
22

33
import (
4+
"bytes"
45
"fmt"
56
"io"
67
"net/http"
@@ -101,12 +102,49 @@ func (m *Downloader) startDownload() tea.Cmd {
101102
m.current = totalDownloaded
102103
}
103104

105+
// Now, check if the file is a valid .tar.lz4 file
106+
if err := m.validateTarLz4Header(); err != nil {
107+
m.done = true
108+
m.err = fmt.Errorf("invalid file format: %w", err)
109+
return nil
110+
}
111+
104112
m.done = true
105113
m.err = nil
106114
return nil
107115
}
108116
}
109117

118+
// LZ4 magic number for LZ4 frame format
119+
var lz4MagicNumber = []byte{0x04, 0x22, 0x4D, 0x18}
120+
121+
// validateTarLz4Header checks if the downloaded file is a valid .tar.lz4 file based on the file header.
122+
func (m *Downloader) validateTarLz4Header() error {
123+
// Open the .lz4 file
124+
file, err := os.Open(m.dest)
125+
if err != nil {
126+
return fmt.Errorf("failed to open file: %w", err)
127+
}
128+
defer file.Close()
129+
130+
// Read the first few bytes of the file (header)
131+
header := make([]byte, 4)
132+
_, err = file.Read(header)
133+
if err != nil {
134+
return fmt.Errorf("failed to read file header: %w", err)
135+
}
136+
137+
// Check if the header matches the LZ4 magic number
138+
if !bytes.Equal(header, lz4MagicNumber) {
139+
return fmt.Errorf("invalid file format: the file is not a valid .lz4 file")
140+
}
141+
142+
// If the header matches, we assume it's a valid .lz4 file.
143+
// You could continue checking the contents further if needed, but this verifies the file is compressed with LZ4.
144+
145+
return nil
146+
}
147+
110148
func (m *Downloader) Init() tea.Cmd {
111149
return tea.Batch(m.tick(), m.startDownload())
112150
}

utils/loading.go

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Loading struct {
3030
quitting bool
3131
frame int
3232
executeFn tea.Cmd
33+
Err error
3334
}
3435

3536
func NewLoading(text string, executeFn tea.Cmd) Loading {
@@ -64,6 +65,9 @@ func (m Loading) Update(msg tea.Msg) (Loading, tea.Cmd) {
6465
case EndLoading:
6566
m.Completing = true
6667
return m, nil
68+
case ErrorLoading:
69+
m.Err = msg.Err
70+
return m, nil
6771
default:
6872
return m, nil
6973
}

0 commit comments

Comments
 (0)