github.com/OpsMx/go-app-base@v0.0.24/httputil/checked_write.go (about)

     1  // Copyright 2022 OpsMx, Inc
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package httputil
    16  
    17  import (
    18  	"io"
    19  	"log"
    20  )
    21  
    22  // CheckedWrite will check to ensure the number of bytes intended to be written
    23  // were written, and log a warning if not.  It will also check the error
    24  // returned and log the error if there was one.
    25  //
    26  // It may be better to try to write the data that wasn't successfully
    27  // written.
    28  //
    29  // This is used to log any errors mostly, rather than try to recover.
    30  func CheckedWrite(w io.Writer, d []byte) {
    31  	l, err := w.Write(d)
    32  	if l != len(d) {
    33  		log.Printf("partial write: %d of %d written", l, len(d))
    34  	}
    35  	if err != nil {
    36  		log.Printf("write failed: %v", err)
    37  	}
    38  }