diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2017-09-04 19:11:14 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2017-09-08 16:55:55 -0400 |
commit | 542c732b94e0a5e7c02fd209a60bd068dbbfa03b (patch) | |
tree | 780a49011174abc3f01f5110fc590d107989518a /nslcd_server | |
parent | 3e1d4d0c562ab99e5a81030a08d224e8174152b9 (diff) |
nslcd_proto: BREAKING CHANGE: Rethink the panic strategy
nslcd_proto.Read() and .Write() panic() with any errors that they may
need to emit. This made composition really simple, I was OK with it
being against the normal Go style.
But, I'm not happy with it anymore; have them return errors now.
This leads us in to nslcd_server.HandleRequest() using those panics
for control flow. Add a maybePanic(error) function to wrap all of the
proto.Read() and proto.Write() calls to restore the panicing behavior.
Diffstat (limited to 'nslcd_server')
-rwxr-xr-x | nslcd_server/func_handlerequest.go.gen | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/nslcd_server/func_handlerequest.go.gen b/nslcd_server/func_handlerequest.go.gen index e7e2dcc..40e00c0 100755 --- a/nslcd_server/func_handlerequest.go.gen +++ b/nslcd_server/func_handlerequest.go.gen @@ -1,6 +1,6 @@ #!/usr/bin/env bash # -*- Mode: Go -*- -# Copyright (C) 2015-2016 Luke Shumaker <lukeshu@sbcglobal.net> +# Copyright (C) 2015-2017 Luke Shumaker <lukeshu@sbcglobal.net> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -35,12 +35,18 @@ import ( const sensitive = "<omitted-from-log>" +func maybePanic(err error) { + if err != nil { + panic(err) + } +} + // Handle a request to nslcd func HandleRequest(backend Backend, in io.Reader, out io.Writer, cred unix.Ucred) (err error) { defer func() { if r := recover(); r != nil { switch r := r.(type) { - case error: + case p.NslcdError: err = r default: panic(r) @@ -53,12 +59,12 @@ func HandleRequest(backend Backend, in io.Reader, out io.Writer, cred unix.Ucred func handleRequest(backend Backend, in io.Reader, out io.Writer, cred unix.Ucred) { var version int32 - p.Read(in, &version) + maybePanic(p.Read(in, &version)) if version != p.NSLCD_VERSION { panic(p.NslcdError(fmt.Sprintf("Version mismatch: server=%#08x client=%#08x", p.NSLCD_VERSION, version))) } var action int32 - p.Read(in, &action) + maybePanic(p.Read(in, &action)) ch := make(chan interface{}) switch action { @@ -67,7 +73,7 @@ while read -r request; do cat <<EOT case p.NSLCD_ACTION_${request^^}: var req p.Request_${request} - p.Read(in, &req) + maybePanic(p.Read(in, &req)) $( case "$request" in PAM_Authentication) @@ -107,13 +113,13 @@ done < "$requests" close(ch) panic(p.NslcdError(fmt.Sprintf("Unknown request action: %#08x", action))) } - p.Write(out, p.NSLCD_VERSION) - p.Write(out, action) + maybePanic(p.Write(out, p.NSLCD_VERSION)) + maybePanic(p.Write(out, action)) for result := range ch { - p.Write(out, p.NSLCD_RESULT_BEGIN) - p.Write(out, result) + maybePanic(p.Write(out, p.NSLCD_RESULT_BEGIN)) + maybePanic(p.Write(out, result)) } - p.Write(out, p.NSLCD_RESULT_END) + maybePanic(p.Write(out, p.NSLCD_RESULT_END)) } EOF |