Changeset 3412


Ignore:
Timestamp:
Apr 23, 2015, 1:00:30 AM (8 years ago)
Author:
Peter
Message:

throw exception rather than returnining empty string when ID or KEY is missing in header. refs #817

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/bam_header.cc

    r3410 r3412  
    143143    suite.err() << "error: exception: " << e.what() << "\n";
    144144  }
     145
     146  try {
     147    // try calling unknown ID
     148    hdr.read_group("banana", "SM");
     149    suite.add(false);
     150    suite.err() << "read_group(\"banana\", \"SM\") didn't throw\n";
     151  }
     152  catch (utility::runtime_error& e) {
     153    suite.out() << "expected exception with what(): " << e.what() << "\n";
     154  }
     155
     156  try {
     157    // try calling unknown key
     158    hdr.read_group("foo", "HM");
     159    suite.add(false);
     160    suite.err() << "read_group(\"foo\", \"HM\") didn't throw\n";
     161  }
     162  catch (utility::runtime_error& e) {
     163    suite.out() << "expected exception with what(): " << e.what() << "\n";
     164  }
    145165#endif
    146166}
  • trunk/yat/omic/BamHeader.cc

    r3410 r3412  
    2626#include "yat/utility/Exception.h"
    2727#include "yat/utility/split.h"
     28#include "yat/utility/stl_utility.h"
    2829
    2930// we need to include 'sam_header.h' when compiling against libbam
     
    99100        }
    100101      }
    101       if (table.empty())
    102         table[""][""]; // to avoid re-parsing
    103     }
    104     return table[id][key];
     102    }
     103    strMap2::const_iterator line_table = table.find(id);
     104    if (line_table == table.end()) {
     105      std::string msg = "@" + type + ": unknown ID: '" + id + "'";
     106      throw utility::runtime_error(msg);
     107    }
     108    strMap::const_iterator it = line_table->second.find(key);
     109    if (it == line_table->second.end()) {
     110      std::string msg = "@" + type + ": unknown KEY: '" + key + "'";
     111      throw utility::runtime_error(msg);
     112    }
     113    return it->second;
    105114  }
    106115
Note: See TracChangeset for help on using the changeset viewer.