« index

Coverage for /Users/yunong/workspace/node-restify/lib/formatters/json.js : 96%

29 lines | 28 run | 1 missing | 0 partial | 5 blocks | 4 blocks run | 1 blocks missing

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

  // Copyright 2012 Mark Cavage, Inc.  All rights reserved.
  
  
  ///--- Exports
  
  function formatJSON(req, res, body) {
      if (body instanceof Error) {
          // snoop for RestError or HttpError, but don't rely on
          // instanceof
          res.statusCode = body.statusCode || 500;
  
          if (body.body) {
              body = body.body;
          } else {
              body = {
                  message: body.message
              };
          }
      } else if (Buffer.isBuffer(body)) {
          body = body.toString('base64');
      }
  
      var data = JSON.stringify(body);
      res.setHeader('Content-Length', Buffer.byteLength(data));
  
      return (data);
  }
  
  module.exports = formatJSON;
« index | cover.io