style fix for error checking (#1170)

+additional linting
This commit is contained in:
Meisam 2021-07-09 20:16:44 +02:00 committed by GitHub
parent a13e1e75e2
commit 7361578412
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 62 additions and 112 deletions

View File

@ -20,8 +20,7 @@ func GetLogs(w http.ResponseWriter, r *http.Request) {
}
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(response)
if err != nil {
if err := json.NewEncoder(w).Encode(response); err != nil {
log.Errorln(err)
}
}
@ -39,8 +38,7 @@ func GetWarnings(w http.ResponseWriter, r *http.Request) {
}
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(response)
if err != nil {
if err := json.NewEncoder(w).Encode(response); err != nil {
log.Errorln(err)
}
}

View File

@ -65,8 +65,7 @@ func GetServerConfig(w http.ResponseWriter, r *http.Request) {
}
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(response)
if err != nil {
if err := json.NewEncoder(w).Encode(response); err != nil {
log.Errorln(err)
}
}

View File

@ -18,8 +18,7 @@ func GetChatMessages(w http.ResponseWriter, r *http.Request) {
case http.MethodGet:
messages := core.GetAllChatMessages()
err := json.NewEncoder(w).Encode(messages)
if err != nil {
if err := json.NewEncoder(w).Encode(messages); err != nil {
log.Errorln(err)
}
default:

View File

@ -112,9 +112,7 @@ func handleScraperMetadataPage(w http.ResponseWriter, r *http.Request) {
}
w.Header().Set("Content-Type", "text/html")
err = tmpl.Execute(w, metadata)
if err != nil {
if err := tmpl.Execute(w, metadata); err != nil {
log.Panicln(err)
}
}

View File

@ -92,24 +92,20 @@ func (c *Client) listenWrite() {
select {
// Send a PING keepalive
case msg := <-c.pingch:
err := websocket.JSON.Send(c.ws, msg)
if err != nil {
if err := websocket.JSON.Send(c.ws, msg); err != nil {
c.handleClientSocketError(err)
}
// send message to the client
case msg := <-c.ch:
err := websocket.JSON.Send(c.ws, msg)
if err != nil {
if err := websocket.JSON.Send(c.ws, msg); err != nil {
c.handleClientSocketError(err)
}
case msg := <-c.usernameChangeChannel:
err := websocket.JSON.Send(c.ws, msg)
if err != nil {
if err := websocket.JSON.Send(c.ws, msg); err != nil {
c.handleClientSocketError(err)
}
case msg := <-c.userJoinedChannel:
err := websocket.JSON.Send(c.ws, msg)
if err != nil {
if err := websocket.JSON.Send(c.ws, msg); err != nil {
c.handleClientSocketError(err)
}
@ -148,8 +144,7 @@ func (c *Client) listenRead() {
// read data from websocket connection
default:
var data []byte
err := websocket.Message.Receive(c.ws, &data)
if err != nil {
if err := websocket.Message.Receive(c.ws, &data); err != nil {
if err == io.EOF {
c.doneCh <- true
return
@ -162,10 +157,9 @@ func (c *Client) listenRead() {
}
var messageTypeCheck map[string]interface{}
err = json.Unmarshal(data, &messageTypeCheck)
// Bad messages should be thrown away
if err != nil {
if err := json.Unmarshal(data, &messageTypeCheck); err != nil {
log.Debugln("Badly formatted message received from", c.Username, c.ws.Request().RemoteAddr)
continue
}
@ -207,8 +201,7 @@ func (c *Client) userJoined(data []byte) {
func (c *Client) userChangedName(data []byte) {
var msg models.NameChangeEvent
err := json.Unmarshal(data, &msg)
if err != nil {
if err := json.Unmarshal(data, &msg); err != nil {
log.Errorln(err)
}
msg.Type = models.UserNameChanged
@ -219,8 +212,7 @@ func (c *Client) userChangedName(data []byte) {
func (c *Client) chatMessageReceived(data []byte) {
var msg models.ChatEvent
err := json.Unmarshal(data, &msg)
if err != nil {
if err := json.Unmarshal(data, &msg); err != nil {
log.Errorln(err)
}

View File

@ -33,8 +33,7 @@ func createTable() {
log.Fatal(err)
}
defer stmt.Close()
_, err = stmt.Exec()
if err != nil {
if _, err := stmt.Exec(); err != nil {
log.Warnln(err)
}
}
@ -51,12 +50,10 @@ func addMessage(message models.ChatEvent) {
}
defer stmt.Close()
_, err = stmt.Exec(message.ID, message.Author, message.Body, message.MessageType, 1, message.Timestamp)
if err != nil {
if _, err := stmt.Exec(message.ID, message.Author, message.Body, message.MessageType, 1, message.Timestamp); err != nil {
log.Fatal(err)
}
err = tx.Commit()
if err != nil {
if err := tx.Commit(); err != nil {
log.Fatal(err)
}
}
@ -133,13 +130,12 @@ func saveMessageVisibility(messageIDs []string, visible bool) error {
args[i+1] = id
}
_, err = stmt.Exec(args...)
if err != nil {
if _, err := stmt.Exec(args...); err != nil {
log.Fatal(err)
return err
}
if err = tx.Commit(); err != nil {
if err := tx.Commit(); err != nil {
log.Fatal(err)
return err
}

View File

@ -25,8 +25,7 @@ func createAccessTokensTable() {
log.Fatal(err)
}
defer stmt.Close()
_, err = stmt.Exec()
if err != nil {
if _, err := stmt.Exec(); err != nil {
log.Warnln(err)
}
}
@ -48,7 +47,7 @@ func InsertToken(token string, name string, scopes []string) error {
}
defer stmt.Close()
if _, err = stmt.Exec(token, name, scopesString); err != nil {
if _, err := stmt.Exec(token, name, scopesString); err != nil {
return err
}

View File

@ -16,8 +16,7 @@ func TestString(t *testing.T) {
const testKey = "test string key"
const testValue = "test string value"
err := _datastore.SetString(testKey, testValue)
if err != nil {
if err := _datastore.SetString(testKey, testValue); err != nil {
panic(err)
}

View File

@ -26,8 +26,7 @@ func createWebhooksTable() {
log.Fatal(err)
}
defer stmt.Close()
_, err = stmt.Exec()
if err != nil {
if _, err = stmt.Exec(); err != nil {
log.Warnln(err)
}
}
@ -126,8 +125,7 @@ func GetWebhooksForEvent(event models.EventType) []models.Webhook {
for rows.Next() {
var url string
err = rows.Scan(&url, &event)
if err != nil {
if err := rows.Scan(&url, &event); err != nil {
log.Debugln(err)
log.Error("There is a problem with the database.")
break

View File

@ -32,16 +32,14 @@ func (s *LocalStorage) Setup() error {
// SegmentWritten is called when a single segment of video is written.
func (s *LocalStorage) SegmentWritten(localFilePath string) {
_, err := s.Save(localFilePath, 0)
if err != nil {
if _, err := s.Save(localFilePath, 0); err != nil {
log.Warnln(err)
}
}
// VariantPlaylistWritten is called when a variant hls playlist is written.
func (s *LocalStorage) VariantPlaylistWritten(localFilePath string) {
_, err := s.Save(localFilePath, 0)
if err != nil {
if _, err := s.Save(localFilePath, 0); err != nil {
log.Errorln(err)
return
}
@ -49,8 +47,7 @@ func (s *LocalStorage) VariantPlaylistWritten(localFilePath string) {
// MasterPlaylistWritten is called when the master hls playlist is written.
func (s *LocalStorage) MasterPlaylistWritten(localFilePath string) {
_, err := s.Save(localFilePath, 0)
if err != nil {
if _, err := s.Save(localFilePath, 0); err != nil {
log.Warnln(err)
}
}

View File

@ -74,8 +74,7 @@ func (s *S3Storage) SegmentWritten(localFilePath string) {
utils.StartPerformanceMonitor(performanceMonitorKey)
// Upload the segment
_, err := s.Save(localFilePath, 0)
if err != nil {
if _, err := s.Save(localFilePath, 0); err != nil {
log.Errorln(err)
return
}
@ -92,8 +91,7 @@ func (s *S3Storage) SegmentWritten(localFilePath string) {
// so the segments and the HLS playlist referencing
// them are in sync.
playlistPath := filepath.Join(filepath.Dir(localFilePath), "stream.m3u8")
_, err = s.Save(playlistPath, 0)
if err != nil {
if _, err := s.Save(playlistPath, 0); err != nil {
_queuedPlaylistUpdates[playlistPath] = playlistPath
if pErr, ok := err.(*os.PathError); ok {
log.Debugln(pErr.Path, "does not yet exist locally when trying to upload to S3 storage.")
@ -108,8 +106,7 @@ func (s *S3Storage) VariantPlaylistWritten(localFilePath string) {
// to make sure we're not referring to files in a playlist that don't
// yet exist. See SegmentWritten.
if _, ok := _queuedPlaylistUpdates[localFilePath]; ok {
_, err := s.Save(localFilePath, 0)
if err != nil {
if _, err := s.Save(localFilePath, 0); err != nil {
log.Errorln(err)
_queuedPlaylistUpdates[localFilePath] = localFilePath
}
@ -120,8 +117,7 @@ func (s *S3Storage) VariantPlaylistWritten(localFilePath string) {
// MasterPlaylistWritten is called when the master hls playlist is written.
func (s *S3Storage) MasterPlaylistWritten(localFilePath string) {
// Rewrite the playlist to use absolute remote S3 URLs
err := s.rewriteRemotePlaylist(localFilePath)
if err != nil {
if err := s.rewriteRemotePlaylist(localFilePath); err != nil {
log.Warnln(err)
}
}
@ -195,8 +191,7 @@ func (s *S3Storage) rewriteRemotePlaylist(filePath string) error {
}
p := m3u8.NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
if err := p.DecodeFrom(bufio.NewReader(f), false); err != nil {
log.Warnln(err)
}

View File

@ -94,12 +94,10 @@ func SetStreamAsDisconnected() {
playlistFilePath := fmt.Sprintf(filepath.Join(config.PrivateHLSStoragePath, "%d/stream.m3u8"), index)
segmentFilePath := fmt.Sprintf(filepath.Join(config.PrivateHLSStoragePath, "%d/%s"), index, offlineFilename)
err := utils.Copy(offlineFilePath, segmentFilePath)
if err != nil {
if err := utils.Copy(offlineFilePath, segmentFilePath); err != nil {
log.Warnln(err)
}
_, err = _storage.Save(segmentFilePath, 0)
if err != nil {
if _, err := _storage.Save(segmentFilePath, 0); err != nil {
log.Warnln(err)
}
if utils.DoesFileExists(playlistFilePath) {
@ -119,16 +117,13 @@ func SetStreamAsDisconnected() {
variantPlaylist.Segments = variantPlaylist.Segments[:len(variantPlaylist.Segments)]
}
err = variantPlaylist.Append(offlineFilename, 8.0, "")
if err != nil {
if err := variantPlaylist.Append(offlineFilename, 8.0, ""); err != nil {
log.Fatalln(err)
}
err = variantPlaylist.SetDiscontinuity()
if err != nil {
if err := variantPlaylist.SetDiscontinuity(); err != nil {
log.Fatalln(err)
}
_, err = f.WriteAt(variantPlaylist.Encode().Bytes(), 0)
if err != nil {
if _, err := f.WriteAt(variantPlaylist.Encode().Bytes(), 0); err != nil {
log.Errorln(err)
}
} else {
@ -138,8 +133,7 @@ func SetStreamAsDisconnected() {
}
// If "offline" content gets changed then change the duration below
err = p.Append(offlineFilename, 8.0, "")
if err != nil {
if err := p.Append(offlineFilename, 8.0, ""); err != nil {
log.Errorln(err)
}
@ -149,13 +143,11 @@ func SetStreamAsDisconnected() {
log.Errorln(err)
}
defer f.Close()
_, err = f.Write(p.Encode().Bytes())
if err != nil {
if _, err := f.Write(p.Encode().Bytes()); err != nil {
log.Errorln(err)
}
}
_, err = _storage.Save(playlistFilePath, 0)
if err != nil {
if _, err := _storage.Save(playlistFilePath, 0); err != nil {
log.Warnln(err)
}
}

View File

@ -74,8 +74,7 @@ func (s *FileWriterReceiverService) uploadHandler(w http.ResponseWriter, r *http
}
defer f.Close()
_, err = f.Write(data)
if err != nil {
if _, err := f.Write(data); err != nil {
returnError(err, w)
return
}

View File

@ -107,8 +107,7 @@ func (t *Transcoder) Start() {
panic(err)
}
err = _commandExec.Start()
if err != nil {
if err := _commandExec.Start(); err != nil {
log.Errorln("Transcoder error. See ", logging.GetTranscoderLogFilePath(), " for full output to debug.")
log.Panicln(err, command)
}

View File

@ -97,28 +97,24 @@ func createVariantDirectories() {
if len(data.GetStreamOutputVariants()) != 0 {
for index := range data.GetStreamOutputVariants() {
err := os.MkdirAll(path.Join(config.PrivateHLSStoragePath, strconv.Itoa(index)), 0777)
if err != nil {
if err := os.MkdirAll(path.Join(config.PrivateHLSStoragePath, strconv.Itoa(index)), 0777); err != nil {
log.Fatalln(err)
}
dir := path.Join(config.PublicHLSStoragePath, strconv.Itoa(index))
log.Traceln("Creating", dir)
err = os.MkdirAll(dir, 0777)
if err != nil {
if err := os.MkdirAll(dir, 0777); err != nil {
log.Fatalln(err)
}
}
} else {
dir := path.Join(config.PrivateHLSStoragePath, strconv.Itoa(0))
log.Traceln("Creating", dir)
err := os.MkdirAll(dir, 0777)
if err != nil {
if err := os.MkdirAll(dir, 0777); err != nil {
log.Fatalln(err)
}
dir = path.Join(config.PublicHLSStoragePath, strconv.Itoa(0))
log.Traceln("Creating", dir)
err = os.MkdirAll(dir, 0777)
if err != nil {
if err := os.MkdirAll(dir, 0777); err != nil {
log.Fatalln(err)
}
}

View File

@ -75,8 +75,7 @@ func main() {
go metrics.Start()
err := data.SetupPersistence(config.DatabaseFilePath)
if err != nil {
if err := data.SetupPersistence(config.DatabaseFilePath); err != nil {
log.Fatalln("failed to open database", err)
}

View File

@ -32,8 +32,7 @@ func Start() error {
// websocket chat server
go func() {
err := chat.Start()
if err != nil {
if err := chat.Start(); err != nil {
log.Fatalln(err)
}
}()

View File

@ -5,8 +5,7 @@ import (
)
func TestCreateAccessToken(t *testing.T) {
_, err := GenerateAccessToken()
if err != nil {
if _, err := GenerateAccessToken(); err != nil {
t.Error(err)
}
}

View File

@ -49,8 +49,7 @@ func Restore(backupFile string, databaseFile string) error {
if err != nil {
return err
}
_, err = db.Exec(rawSql)
if err != nil {
if _, err := db.Exec(rawSql); err != nil {
return err
}

View File

@ -161,8 +161,7 @@ func GetCacheDurationSecondsForPath(filePath string) int {
}
func IsValidUrl(urlToTest string) bool {
_, err := url.ParseRequestURI(urlToTest)
if err != nil {
if _, err := url.ParseRequestURI(urlToTest); err != nil {
return false
}

View File

@ -113,8 +113,7 @@ func (yp *YP) ping() {
}
pingResponse := ypPingResponse{}
err = json.Unmarshal(body, &pingResponse)
if err != nil {
if err := json.Unmarshal(body, &pingResponse); err != nil {
log.Errorln(err)
}