replace panic with error, handle missing error case