Replace panic with CloseWithError.

bot-api-6.1
Syfaro 2020-07-25 23:36:31 -05:00
parent 99b74b8efa
commit c6bf64c67d
1 changed files with 18 additions and 9 deletions

27
bot.go
View File

@ -160,7 +160,8 @@ func (bot *BotAPI) UploadFiles(endpoint string, params Params, files []RequestFi
for field, value := range params { for field, value := range params {
if err := m.WriteField(field, value); err != nil { if err := m.WriteField(field, value); err != nil {
panic(err) w.CloseWithError(err)
return
} }
} }
@ -169,20 +170,23 @@ func (bot *BotAPI) UploadFiles(endpoint string, params Params, files []RequestFi
case string: case string:
fileHandle, err := os.Open(f) fileHandle, err := os.Open(f)
if err != nil { if err != nil {
panic(err) w.CloseWithError(err)
return
} }
defer fileHandle.Close() defer fileHandle.Close()
part, err := m.CreateFormFile(file.Name, fileHandle.Name()) part, err := m.CreateFormFile(file.Name, fileHandle.Name())
if err != nil { if err != nil {
panic(err) w.CloseWithError(err)
return
} }
io.Copy(part, fileHandle) io.Copy(part, fileHandle)
case FileBytes: case FileBytes:
part, err := m.CreateFormFile(file.Name, f.Name) part, err := m.CreateFormFile(file.Name, f.Name)
if err != nil { if err != nil {
panic(err) w.CloseWithError(err)
return
} }
buf := bytes.NewBuffer(f.Bytes) buf := bytes.NewBuffer(f.Bytes)
@ -190,7 +194,8 @@ func (bot *BotAPI) UploadFiles(endpoint string, params Params, files []RequestFi
case FileReader: case FileReader:
part, err := m.CreateFormFile(file.Name, f.Name) part, err := m.CreateFormFile(file.Name, f.Name)
if err != nil { if err != nil {
panic(err) w.CloseWithError(err)
return
} }
if f.Size != -1 { if f.Size != -1 {
@ -198,7 +203,8 @@ func (bot *BotAPI) UploadFiles(endpoint string, params Params, files []RequestFi
} else { } else {
data, err := ioutil.ReadAll(f.Reader) data, err := ioutil.ReadAll(f.Reader)
if err != nil { if err != nil {
panic(err) w.CloseWithError(err)
return
} }
buf := bytes.NewBuffer(data) buf := bytes.NewBuffer(data)
@ -207,15 +213,18 @@ func (bot *BotAPI) UploadFiles(endpoint string, params Params, files []RequestFi
case FileURL: case FileURL:
val := string(f) val := string(f)
if err := m.WriteField(file.Name, val); err != nil { if err := m.WriteField(file.Name, val); err != nil {
panic(err) w.CloseWithError(err)
return
} }
case FileID: case FileID:
val := string(f) val := string(f)
if err := m.WriteField(file.Name, val); err != nil { if err := m.WriteField(file.Name, val); err != nil {
panic(err) w.CloseWithError(err)
return
} }
default: default:
panic(errors.New(ErrBadFileType)) w.CloseWithError(errors.New(ErrBadFileType))
return
} }
} }
}() }()