如何在ruby脚本中获取致命错误的确切消息

时间:2019-06-28 10:35:35

标签: ruby postgresql pg-dump pg-restore

我有一个Ruby脚本,该脚本从远程服务器提取 PostgreSQL数据库转储。脚本需要动态输入以下内容:密码,用户名,主机和数据库名称。因此,用户可能会为这些字段提供错误的输入。如果输入有任何错误,我会收到致命错误,如

  

pg_dump:[存档器(db)]与数据库“ sc_development1”的连接   失败:致命:数据库“ sc_development1”不存在

如果我们这样做时数据库错误,

    <html>
      <head>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.js"></script>
      </head>

      <body>
        <canvas id="myChart" width="400" height="400"></canvas>


        <script>
        
        //your simple array
        var arr = [65, 59, 80];
          // Get the context of the canvas element we want to select
    var data = {
        labels: ['Krabby Pattie', 'Krusty Combo', 'Krusty Deluxe'],
        datasets: [
            {
                label: "My First dataset",
                fillColor: "rgba(220,220,220,0.2)",
                strokeColor: "rgba(220,220,220,1)",
                pointColor: "rgba(220,220,220,1)",
                pointStrokeColor: "#fff",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(220,220,220,1)",
                data:arr
            },
            
        ]
    };

    var ctx = document.getElementById("myChart").getContext("2d");
    var myBarChart = new Chart(ctx).Bar(data, {
        //Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
        scaleBeginAtZero : true,

        //Boolean - Whether grid lines are shown across the chart
        scaleShowGridLines : true,

        //String - Colour of the grid lines
        scaleGridLineColor : "rgba(0, 0, 0, .05)",

        //Number - Width of the grid lines
        scaleGridLineWidth : 1,

        //Boolean - Whether to show horizontal lines (except X axis)
        scaleShowHorizontalLines: true,

        //Boolean - Whether to show vertical lines (except Y axis)
        scaleShowVerticalLines: true,

        //Boolean - If there is a stroke on each bar
        barShowStroke : true,

        //Number - Pixel width of the bar stroke
        barStrokeWidth : 2,

        //Number - Spacing between each of the X value sets
        barValueSpacing : 5,

        //Number - Spacing between data sets within X values
        barDatasetSpacing : 1,

        //String - A legend template
        legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].fillColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"

    });;

        </script>
      </body>

    </html>

当前,我正在通过检查system("PGPASSWORD="#{source_postgres_password}" pg_dump -U "#{source_postgres_username}" -h "#{source_host}" "#{source_database_name}" > "#{store_backup_file_path}/#{timestamp}/#{source_database_name}".sql") 来获取进程状态,但是我想要更多。我想获取确切的致命错误消息以在日志文件中打印。该怎么做?

我想打印从终端运行脚本到日志文件的确切致命错误消息。

有什么办法可以给我致命错误的详细信息。我知道在Ruby中挽救致命错误是不可能的。当我阅读this时,我可以打印自己的消息,以防发生致命错误,但这不是我想要的。我想要确切的致命错误消息。

1 个答案:

答案 0 :(得分:4)

您可以使用open3模块和<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>User_Test_Series-1</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>Registration</servlet-name> <servlet-class>jdbc.Registration</servlet-class> </servlet> <servlet-mapping> <servlet-name>Registration</servlet-name> <url-pattern>/Registration</url-pattern> </servlet-mapping> </web-app> 功能来捕获标准输出,标准错误和状态:

capture3

根据您的情况做

[3] pry(main)> require 'open3'
=> true
[4] pry(main)> stdout, stderr, status = Open3.capture3("ls asd")
=> ["", "ls: cannot access 'asd': No such file or directory\n", #<Process::Status: pid 19314 exit 2>]

另请参阅:https://www.honeybadger.io/blog/capturing-stdout-stderr-from-shell-commands-via-ruby/